forked from kptdev/kpt
-
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.
rollouts: add package cluster matcher (kptdev#3700)
- Loading branch information
1 parent
c07d23c
commit 9f66602
Showing
11 changed files
with
248 additions
and
9 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,27 @@ | ||
# Copyright 2022 Google LLC | ||
# | ||
# Licensed 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. | ||
|
||
apiVersion: container.cnrm.cloud.google.com/v1beta1 | ||
kind: ContainerCluster | ||
metadata: | ||
name: containercluster-maui | ||
namespace: config-control | ||
labels: | ||
location/island: maui | ||
spec: | ||
description: Maui autopilot cluster | ||
enableAutopilot: true | ||
location: us-central1 | ||
releaseChannel: | ||
channel: REGULAR |
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,27 @@ | ||
# Copyright 2022 Google LLC | ||
# | ||
# Licensed 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. | ||
|
||
apiVersion: container.cnrm.cloud.google.com/v1beta1 | ||
kind: ContainerCluster | ||
metadata: | ||
name: containercluster-oahu | ||
namespace: config-control | ||
labels: | ||
location/island: oahu | ||
spec: | ||
description: Oahu autopilot cluster | ||
enableAutopilot: true | ||
location: us-central1 | ||
releaseChannel: | ||
channel: REGULAR |
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
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
118 changes: 118 additions & 0 deletions
118
rollouts/pkg/packageclustermatcher/packageclustermatcher.go
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,118 @@ | ||
// Copyright 2022 Google LLC | ||
// | ||
// Licensed 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 packageclustermatcher | ||
|
||
import ( | ||
"fmt" | ||
|
||
gkeclusterapis "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/clients/generated/apis/container/v1beta1" | ||
gitopsv1alpha1 "github.com/GoogleContainerTools/kpt/rollouts/api/v1alpha1" | ||
"github.com/GoogleContainerTools/kpt/rollouts/pkg/packagediscovery" | ||
|
||
"github.com/google/cel-go/cel" | ||
"github.com/google/cel-go/checker/decls" | ||
) | ||
|
||
type PackageClusterMatcher struct { | ||
clusters []gkeclusterapis.ContainerCluster | ||
packages []packagediscovery.DiscoveredPackage | ||
} | ||
|
||
type ClusterPackages struct { | ||
Cluster gkeclusterapis.ContainerCluster | ||
Packages []packagediscovery.DiscoveredPackage | ||
} | ||
|
||
func NewPackageClusterMatcher(clusters []gkeclusterapis.ContainerCluster, packages []packagediscovery.DiscoveredPackage) *PackageClusterMatcher { | ||
return &PackageClusterMatcher{ | ||
clusters: clusters, | ||
packages: packages, | ||
} | ||
} | ||
|
||
func (m *PackageClusterMatcher) GetClusterPackages(matcher gitopsv1alpha1.PackageToClusterMatcher) ([]ClusterPackages, error) { | ||
clusters := m.clusters | ||
packages := m.packages | ||
|
||
allClusterPackages := []ClusterPackages{} | ||
|
||
for _, cluster := range clusters { | ||
matchedPackages := []packagediscovery.DiscoveredPackage{} | ||
|
||
celCluster := map[string]interface{}{ | ||
"name": cluster.ObjectMeta.Name, | ||
"labels": cluster.ObjectMeta.Labels, | ||
} | ||
|
||
for _, discoveredPackage := range packages { | ||
celPackage := map[string]interface{}{ | ||
"org": discoveredPackage.Org, | ||
"repo": discoveredPackage.Repo, | ||
"directory": discoveredPackage.Directory, | ||
} | ||
|
||
isMatch, err := isPackageClusterMatch(matcher.MatchExpression, celCluster, celPackage) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to execute package cluster matcher expression: %w", err) | ||
} | ||
|
||
if isMatch { | ||
matchedPackages = append(matchedPackages, discoveredPackage) | ||
} | ||
} | ||
|
||
clusterPackages := ClusterPackages{ | ||
Cluster: cluster, | ||
Packages: matchedPackages, | ||
} | ||
|
||
allClusterPackages = append(allClusterPackages, clusterPackages) | ||
} | ||
|
||
return allClusterPackages, nil | ||
} | ||
|
||
func isPackageClusterMatch(expr string, cluster, rolloutPackage map[string]interface{}) (bool, error) { | ||
env, err := cel.NewEnv( | ||
cel.Declarations( | ||
decls.NewVar("cluster", decls.Dyn), | ||
decls.NewVar("rolloutPackage", decls.Dyn), | ||
)) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
p, issue := env.Parse(expr) | ||
if issue != nil && issue.Err() != nil { | ||
return false, issue.Err() | ||
} | ||
|
||
c, issue := env.Check(p) | ||
if issue != nil && issue.Err() != nil { | ||
return false, issue.Err() | ||
} | ||
|
||
prg, err := env.Program(c) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
out, _, err := prg.Eval(map[string]interface{}{ | ||
"cluster": cluster, | ||
"rolloutPackage": rolloutPackage, | ||
}) | ||
|
||
return out.Value().(bool), err | ||
} |
Oops, something went wrong.