diff --git a/Dockerfile.rhtap b/Dockerfile.rhtap index 7dbde8c4..9613e26a 100644 --- a/Dockerfile.rhtap +++ b/Dockerfile.rhtap @@ -3,13 +3,17 @@ FROM brew.registry.redhat.io/rh-osbs/openshift-golang-builder:rhel_9_1.22 AS builder COPY . . RUN chmod g+w . && \ - git config --global --add safe.directory "$PWD" -RUN GOFLAGS="" go build ./cmd/manager -RUN GOFLAGS="" go test -covermode=atomic -coverpkg=github.com/stolostron/klusterlet-addon-controller/pkg/... -c -tags testrunmain ./cmd/manager -o manager-coverage + git config --global --add safe.directory "$PWD" && \ + GOFLAGS="" go build ./cmd/manager && \ + GOFLAGS="" go test \ + -covermode=atomic \ + -coverpkg=github.com/stolostron/klusterlet-addon-controller/pkg/... \ + -c -tags testrunmain ./cmd/manager \ + -o manager-coverage FROM registry.access.redhat.com/ubi9/ubi-minimal:latest -RUN microdnf update && \ +RUN microdnf update -y && \ microdnf clean all ENV IMAGE_MANIFEST_PATH=/ diff --git a/build/Dockerfile b/build/Dockerfile index 98fd5a88..daf70f59 100644 --- a/build/Dockerfile +++ b/build/Dockerfile @@ -8,12 +8,17 @@ ARG REMOTE_SOURCE_DIR COPY $REMOTE_SOURCE $REMOTE_SOURCE_DIR/app/ WORKDIR $REMOTE_SOURCE_DIR/app -RUN GOFLAGS="" go build ./cmd/manager -RUN GOFLAGS="" go test -covermode=atomic -coverpkg=github.com/stolostron/klusterlet-addon-controller/pkg/... -c -tags testrunmain ./cmd/manager -o manager-coverage +RUN GOFLAGS="" go build ./cmd/manager && \ + GOFLAGS="" go test \ + -covermode=atomic \ + -coverpkg=github.com/stolostron/klusterlet-addon-controller/pkg/... \ + -c -tags testrunmain \ + ./cmd/manager \ + -o manager-coverage FROM registry.access.redhat.com/ubi9/ubi-minimal:latest -RUN microdnf update && \ +RUN microdnf update -y && \ microdnf clean all ARG REMOTE_SOURCE_DIR diff --git a/build/Dockerfile-coverage b/build/Dockerfile-coverage index 1848ba4e..bd8b1e12 100644 --- a/build/Dockerfile-coverage +++ b/build/Dockerfile-coverage @@ -12,7 +12,7 @@ ENV OPERATOR=/usr/local/bin/klusterlet-addon-controller \ USER root # Install unzip -RUN microdnf update +RUN microdnf update -y RUN microdnf install -y --nodocs jq RUN microdnf install -y --nodocs openssl diff --git a/pkg/controller/addon/klusterlet_addon_controller.go b/pkg/controller/addon/klusterlet_addon_controller.go index 9a9bf2c5..3357f830 100644 --- a/pkg/controller/addon/klusterlet_addon_controller.go +++ b/pkg/controller/addon/klusterlet_addon_controller.go @@ -7,6 +7,7 @@ import ( "reflect" "strings" + "github.com/stolostron/cluster-lifecycle-api/helpers/localcluster" imageregistryv1alpha1 "github.com/stolostron/cluster-lifecycle-api/imageregistry/v1alpha1" agentv1 "github.com/stolostron/klusterlet-addon-controller/pkg/apis/agent/v1" "github.com/stolostron/klusterlet-addon-controller/pkg/common" @@ -224,7 +225,7 @@ func isPaused(instance *agentv1.KlusterletAddonConfig) bool { func getNodeSelector(managedCluster *managedclusterv1.ManagedCluster) (map[string]string, error) { var nodeSelector map[string]string - if managedCluster.GetName() == "local-cluster" { + if localcluster.IsClusterSelfManaged(managedCluster) { annotations := managedCluster.GetAnnotations() if nodeSelectorString, ok := annotations[annotationNodeSelector]; ok { if err := json.Unmarshal([]byte(nodeSelectorString), &nodeSelector); err != nil { diff --git a/pkg/controller/addon/klusterlet_addon_controller_test.go b/pkg/controller/addon/klusterlet_addon_controller_test.go index 6061817a..01b28cc4 100644 --- a/pkg/controller/addon/klusterlet_addon_controller_test.go +++ b/pkg/controller/addon/klusterlet_addon_controller_test.go @@ -7,6 +7,7 @@ import ( "reflect" "testing" + apiconstants "github.com/stolostron/cluster-lifecycle-api/constants" "github.com/stolostron/klusterlet-addon-controller/pkg/apis" agentv1 "github.com/stolostron/klusterlet-addon-controller/pkg/apis/agent/v1" v1 "github.com/stolostron/klusterlet-addon-controller/pkg/apis/agent/v1" @@ -165,10 +166,11 @@ func newKlusterletAddonConfigWithProxy(clusterName string) *v1.KlusterletAddonCo } } -func newManagedCluster(name string, annotations map[string]string) *mcv1.ManagedCluster { +func newManagedCluster(name string, labels, annotations map[string]string) *mcv1.ManagedCluster { return &mcv1.ManagedCluster{ ObjectMeta: metav1.ObjectMeta{ Name: name, + Labels: labels, Annotations: annotations, }, } @@ -192,7 +194,7 @@ func Test_Reconcile(t *testing.T) { { name: "cluster is created, create all addons", clusterName: "cluster1", - managedCluster: newManagedCluster("cluster1", nil), + managedCluster: newManagedCluster("cluster1", nil, nil), klusterletAddonConfig: newKlusterletAddonConfig("cluster1"), validateFunc: func(t *testing.T, kubeClient client.Client) { addonList := &v1alpha1.ManagedClusterAddOnList{} @@ -208,7 +210,7 @@ func Test_Reconcile(t *testing.T) { { name: "cluster is created in hosed mode with hosted add-on enabled", clusterName: "cluster1", - managedCluster: newManagedCluster("cluster1", map[string]string{ + managedCluster: newManagedCluster("cluster1", nil, map[string]string{ common.AnnotationKlusterletDeployMode: "Hosted", common.AnnotationKlusterletHostingClusterName: "local-cluster", common.AnnotationEnableHostedModeAddons: "true", @@ -248,7 +250,7 @@ func Test_Reconcile(t *testing.T) { { name: "no klusterletaddonconfig", clusterName: "cluster1", - managedCluster: newManagedCluster("cluster1", nil), + managedCluster: newManagedCluster("cluster1", nil, nil), validateFunc: func(t *testing.T, kubeClient client.Client) { addonList := &v1alpha1.ManagedClusterAddOnList{} err := kubeClient.List(context.TODO(), addonList, &client.ListOptions{Namespace: "cluster1"}) @@ -262,14 +264,16 @@ func Test_Reconcile(t *testing.T) { }, { name: "local-cluster with annotations", - clusterName: "local-cluster", - managedCluster: newManagedCluster("local-cluster", map[string]string{ + clusterName: "local-cluster-test", + managedCluster: newManagedCluster("local-cluster-test", map[string]string{ + apiconstants.SelfManagedClusterLabelKey: "true", + }, map[string]string{ annotationNodeSelector: `{"node":"infra"}`, }), - klusterletAddonConfig: newKlusterletAddonConfig("local-cluster"), + klusterletAddonConfig: newKlusterletAddonConfig("local-cluster-test"), validateFunc: func(t *testing.T, kubeClient client.Client) { addonList := &v1alpha1.ManagedClusterAddOnList{} - err := kubeClient.List(context.TODO(), addonList, &client.ListOptions{Namespace: "local-cluster"}) + err := kubeClient.List(context.TODO(), addonList, &client.ListOptions{Namespace: "local-cluster-test"}) if err != nil { t.Errorf("faild to list addons. %v", err) } @@ -295,7 +299,7 @@ func Test_Reconcile(t *testing.T) { { name: "cluster with proxy", clusterName: "cluster1", - managedCluster: newManagedCluster("cluster1", nil), + managedCluster: newManagedCluster("cluster1", nil, nil), klusterletAddonConfig: newKlusterletAddonConfigWithProxy("cluster1"), managedClusterAddons: []runtime.Object{ newManagedClusterAddon(v1.ApplicationAddonName, "cluster1", ""), @@ -331,7 +335,7 @@ func Test_Reconcile(t *testing.T) { { name: "upgrade remove iam addon", clusterName: "cluster1", - managedCluster: newManagedCluster("cluster1", nil), + managedCluster: newManagedCluster("cluster1", nil, nil), klusterletAddonConfig: newKlusterletAddonConfig("cluster1"), managedClusterAddons: []runtime.Object{ newManagedClusterAddon(v1.IamPolicyAddonName, "cluster1", ""),