forked from apache/dubbo-kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request apache#133 from ev1lQuark/cache-selector
feat: add support for cache selection by service
- Loading branch information
Showing
5 changed files
with
365 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package selector | ||
|
||
import ( | ||
"github.com/apache/dubbo-kubernetes/pkg/admin/constant" | ||
"k8s.io/apimachinery/pkg/labels" | ||
) | ||
|
||
type ApplicationSelector struct { | ||
Name string | ||
} | ||
|
||
func NewApplicationSelector(name string) *ApplicationSelector { | ||
return &ApplicationSelector{ | ||
Name: name, | ||
} | ||
} | ||
|
||
func (s *ApplicationSelector) AsLabelsSelector() labels.Selector { | ||
selector := labels.Set{ | ||
constant.ApplicationLabel: s.Name, | ||
} | ||
return selector.AsSelector() | ||
} | ||
|
||
func (s *ApplicationSelector) ApplicationOptions() (Options, bool) { | ||
return newOptions(s.Name), true | ||
} | ||
|
||
func (s *ApplicationSelector) ServiceNameOptions() (Options, bool) { | ||
return nil, false | ||
} | ||
|
||
func (s *ApplicationSelector) ServiceGroupOptions() (Options, bool) { | ||
return nil, false | ||
} | ||
|
||
func (s *ApplicationSelector) ServiceVersionOptions() (Options, bool) { | ||
return nil, false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package selector | ||
|
||
import ( | ||
"github.com/apache/dubbo-kubernetes/pkg/admin/constant" | ||
"github.com/apache/dubbo-kubernetes/pkg/core/logger" | ||
"k8s.io/apimachinery/pkg/labels" | ||
"k8s.io/apimachinery/pkg/selection" | ||
) | ||
|
||
// MultiSelectors is an implement of Selector to combine multiple selectors, use NewMultiSelector to create it, and use Add to build it | ||
type MultiSelectors struct { | ||
applicationNames []string | ||
serviceNames []string | ||
serviceGroups []string | ||
serviceVersions []string | ||
} | ||
|
||
func NewMultiSelector() *MultiSelectors { | ||
return &MultiSelectors{ | ||
applicationNames: make([]string, 0), | ||
serviceNames: make([]string, 0), | ||
serviceGroups: make([]string, 0), | ||
serviceVersions: make([]string, 0), | ||
} | ||
} | ||
|
||
func (s *MultiSelectors) Add(selector Selector) *MultiSelectors { | ||
switch selector.(type) { | ||
case *ApplicationSelector: | ||
s.applicationNames = append(s.applicationNames, selector.(*ApplicationSelector).Name) | ||
case *ServiceSelector: | ||
s.serviceNames = append(s.serviceNames, selector.(*ServiceSelector).Name) | ||
if selector.(*ServiceSelector).Group != "" { | ||
s.serviceGroups = append(s.serviceGroups, selector.(*ServiceSelector).Group) | ||
} | ||
if selector.(*ServiceSelector).Version != "" { | ||
s.serviceVersions = append(s.serviceVersions, selector.(*ServiceSelector).Version) | ||
} | ||
} | ||
return s | ||
} | ||
|
||
func (s *MultiSelectors) AsLabelsSelector() labels.Selector { | ||
requirements := make([]labels.Requirement, 0) | ||
|
||
if len(s.applicationNames) > 0 { | ||
req, err := labels.NewRequirement(constant.ApplicationLabel, selection.In, s.applicationNames) | ||
if err != nil { | ||
logger.Errorf("failed to create requirement for application selector: %v", err) | ||
} | ||
requirements = append(requirements, *req) | ||
} | ||
|
||
if len(s.serviceNames) > 0 { | ||
req, err := labels.NewRequirement(constant.ServiceKeyLabel, selection.In, s.serviceNames) | ||
if err != nil { | ||
logger.Errorf("failed to create requirement for service selector: %v", err) | ||
} | ||
requirements = append(requirements, *req) | ||
} | ||
|
||
if len(s.serviceGroups) > 0 { | ||
req, err := labels.NewRequirement(constant.GroupLabel, selection.In, s.serviceGroups) | ||
if err != nil { | ||
logger.Errorf("failed to create requirement for group selector: %v", err) | ||
} | ||
requirements = append(requirements, *req) | ||
} | ||
|
||
if len(s.serviceVersions) > 0 { | ||
req, err := labels.NewRequirement(constant.VersionLabel, selection.In, s.serviceVersions) | ||
if err != nil { | ||
logger.Errorf("failed to create requirement for version selector: %v", err) | ||
} | ||
requirements = append(requirements, *req) | ||
} | ||
|
||
return labels.NewSelector().Add(requirements...) | ||
} | ||
|
||
func (s *MultiSelectors) ApplicationOptions() (Options, bool) { | ||
if len(s.applicationNames) == 0 { | ||
return nil, false | ||
} | ||
return newOptions(s.applicationNames...), true | ||
} | ||
|
||
func (s *MultiSelectors) ServiceNameOptions() (Options, bool) { | ||
if len(s.serviceNames) == 0 { | ||
return nil, false | ||
} | ||
return newOptions(s.serviceNames...), true | ||
} | ||
|
||
func (s *MultiSelectors) ServiceGroupOptions() (Options, bool) { | ||
if len(s.serviceGroups) == 0 { | ||
return nil, false | ||
} | ||
return newOptions(s.serviceGroups...), true | ||
} | ||
|
||
func (s *MultiSelectors) ServiceVersionOptions() (Options, bool) { | ||
if len(s.serviceVersions) == 0 { | ||
return nil, false | ||
} | ||
return newOptions(s.serviceVersions...), true | ||
} |
Oops, something went wrong.