-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ClusterAnnotations support. (#234)
Signed-off-by: xuezhaojun <zxue@redhat.com>
- Loading branch information
1 parent
70d6112
commit 142fd5b
Showing
21 changed files
with
249 additions
and
7 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
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
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,26 @@ | ||
package helpers | ||
|
||
import ( | ||
"strings" | ||
|
||
"k8s.io/klog/v2" | ||
|
||
operatorv1 "open-cluster-management.io/api/operator/v1" | ||
) | ||
|
||
func FilterClusterAnnotations(annotations map[string]string) map[string]string { | ||
clusterAnnotations := make(map[string]string) | ||
if annotations == nil { | ||
return clusterAnnotations | ||
} | ||
|
||
for k, v := range annotations { | ||
if strings.HasPrefix(k, operatorv1.ClusterAnnotationsKeyPrefix) { | ||
clusterAnnotations[k] = v | ||
} else { | ||
klog.Warningf("annotation %q is not prefixed with %q, it will be ignored", k, operatorv1.ClusterAnnotationsKeyPrefix) | ||
} | ||
} | ||
|
||
return clusterAnnotations | ||
} |
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,71 @@ | ||
package helpers | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
operatorv1 "open-cluster-management.io/api/operator/v1" | ||
) | ||
|
||
func TestFilterClusterAnnotations(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
annotations map[string]string | ||
want map[string]string | ||
}{ | ||
{ | ||
name: "empty annotations", | ||
annotations: map[string]string{}, | ||
want: map[string]string{}, | ||
}, | ||
{ | ||
name: "no cluster annotations", | ||
annotations: map[string]string{ | ||
"foo": "bar", | ||
"baz": "qux", | ||
}, | ||
want: map[string]string{}, | ||
}, | ||
{ | ||
name: "one cluster annotation", | ||
annotations: map[string]string{ | ||
operatorv1.ClusterAnnotationsKeyPrefix + "foo": "bar", | ||
"baz": "qux", | ||
}, | ||
want: map[string]string{ | ||
operatorv1.ClusterAnnotationsKeyPrefix + "foo": "bar", | ||
}, | ||
}, | ||
{ | ||
name: "multiple cluster annotations", | ||
annotations: map[string]string{ | ||
operatorv1.ClusterAnnotationsKeyPrefix + "foo": "bar", | ||
operatorv1.ClusterAnnotationsKeyPrefix + "baz": "qux", | ||
"quux": "corge", | ||
}, | ||
want: map[string]string{ | ||
operatorv1.ClusterAnnotationsKeyPrefix + "foo": "bar", | ||
operatorv1.ClusterAnnotationsKeyPrefix + "baz": "qux", | ||
}, | ||
}, | ||
{ | ||
name: "all annotations are cluster annotations", | ||
annotations: map[string]string{ | ||
operatorv1.ClusterAnnotationsKeyPrefix + "foo": "bar", | ||
operatorv1.ClusterAnnotationsKeyPrefix + "baz": "qux", | ||
}, | ||
want: map[string]string{ | ||
operatorv1.ClusterAnnotationsKeyPrefix + "foo": "bar", | ||
operatorv1.ClusterAnnotationsKeyPrefix + "baz": "qux", | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := FilterClusterAnnotations(tt.annotations); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("FilterClusterAnnotations() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package registration_test | ||
|
||
import ( | ||
"fmt" | ||
"path" | ||
"time" | ||
|
||
"github.com/onsi/ginkgo/v2" | ||
"github.com/onsi/gomega" | ||
|
||
commonoptions "open-cluster-management.io/ocm/pkg/common/options" | ||
"open-cluster-management.io/ocm/pkg/registration/spoke" | ||
"open-cluster-management.io/ocm/test/integration/util" | ||
) | ||
|
||
var _ = ginkgo.Describe("Cluster Annotations", func() { | ||
ginkgo.It("Cluster Annotations should be created on the managed cluster", func() { | ||
managedClusterName := "clusterannotations-spokecluster" | ||
//#nosec G101 | ||
hubKubeconfigSecret := "clusterannotations-hub-kubeconfig-secret" | ||
hubKubeconfigDir := path.Join(util.TestDir, "clusterannotations", "hub-kubeconfig") | ||
|
||
agentOptions := &spoke.SpokeAgentOptions{ | ||
BootstrapKubeconfig: bootstrapKubeConfigFile, | ||
HubKubeconfigSecret: hubKubeconfigSecret, | ||
ClusterHealthCheckPeriod: 1 * time.Minute, | ||
ClusterAnnotations: map[string]string{ | ||
"agent.open-cluster-management.io/foo": "bar", | ||
"foo": "bar", // this annotation should be filtered out | ||
}, | ||
} | ||
|
||
commOptions := commonoptions.NewAgentOptions() | ||
commOptions.HubKubeconfigDir = hubKubeconfigDir | ||
commOptions.SpokeClusterName = managedClusterName | ||
|
||
// run registration agent | ||
cancel := runAgent("rotationtest", agentOptions, commOptions, spokeCfg) | ||
defer cancel() | ||
|
||
// after bootstrap the spokecluster and csr should be created | ||
gomega.Eventually(func() error { | ||
mc, err := util.GetManagedCluster(clusterClient, managedClusterName) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if len(mc.Annotations) != 1 { | ||
return fmt.Errorf("expected 1 annotation, got %d", len(mc.Annotations)) | ||
} | ||
|
||
if mc.Annotations["agent.open-cluster-management.io/foo"] != "bar" { | ||
return fmt.Errorf("expected annotation agent.open-cluster-management.io/foo to be bar, got %s", mc.Annotations["agent.open-cluster-management.io/foo"]) | ||
} | ||
return nil | ||
}, eventuallyTimeout, eventuallyInterval).Should(gomega.Succeed()) | ||
|
||
}) | ||
}) |
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
5 changes: 5 additions & 0 deletions
5
...ement.io/api/crdsv1beta1/0001_00_operator.open-cluster-management.io_klusterlets.crd.yaml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.