From 92a0634ca52c4702e2b67d43c3db396a410bc272 Mon Sep 17 00:00:00 2001 From: Danielle Barda Date: Wed, 30 Sep 2020 13:37:14 +0300 Subject: [PATCH] Changing openshift provider to be kubevirt provider --- pkg/{ocp => kubevirt}/actuator.go | 44 +++++++++++++------------- pkg/{ocp => kubevirt}/actuator_test.go | 20 ++++++------ pkg/operator/controller.go | 7 ++-- 3 files changed, 35 insertions(+), 36 deletions(-) rename pkg/{ocp => kubevirt}/actuator.go (77%) rename pkg/{ocp => kubevirt}/actuator_test.go (95%) diff --git a/pkg/ocp/actuator.go b/pkg/kubevirt/actuator.go similarity index 77% rename from pkg/ocp/actuator.go rename to pkg/kubevirt/actuator.go index 1cbd91f8a1..c774d12475 100644 --- a/pkg/ocp/actuator.go +++ b/pkg/kubevirt/actuator.go @@ -13,7 +13,7 @@ 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 ocp +package kubevirt import ( "context" @@ -35,7 +35,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -type OCPActuator struct { +type KubevirtActuator struct { Client client.Client Codec *minterv1.ProviderCodec } @@ -43,15 +43,15 @@ type OCPActuator struct { const ( KubevirtCredentialsSecretKey = "kubeconfig" ) -// NewActuator creates a new OCP actuator. -func NewActuator(client client.Client) (*OCPActuator, error) { +// NewActuator creates a new Kubevirt actuator. +func NewActuator(client client.Client) (*KubevirtActuator, error) { codec, err := minterv1.NewCodec() if err != nil { - log.WithError(err).Error("error creating OCP codec") - return nil, fmt.Errorf("error creating OCP codec: %v", err) + log.WithError(err).Error("error creating Kubevirt codec") + return nil, fmt.Errorf("error creating Kubevirt codec: %v", err) } - return &OCPActuator{ + return &KubevirtActuator{ Codec: codec, Client: client, }, nil @@ -59,7 +59,7 @@ func NewActuator(client client.Client) (*OCPActuator, error) { // Exists checks if the credentials currently exist. // TODO: in the future validate the expiration of the credentials -func (a *OCPActuator) Exists(ctx context.Context, cr *minterv1.CredentialsRequest) (bool, error) { +func (a *KubevirtActuator) Exists(ctx context.Context, cr *minterv1.CredentialsRequest) (bool, error) { logger := a.getLogger(cr) logger.Debug("running Exists") var err error @@ -73,21 +73,21 @@ func (a *OCPActuator) Exists(ctx context.Context, cr *minterv1.CredentialsReques } // Create the credentials. -func (a *OCPActuator) Create(ctx context.Context, cr *minterv1.CredentialsRequest) error { +func (a *KubevirtActuator) Create(ctx context.Context, cr *minterv1.CredentialsRequest) error { logger := a.getLogger(cr) logger.Debug("running Create") return a.sync(ctx, cr, logger) } // Update the credentials to the provided definition. -func (a *OCPActuator) Update(ctx context.Context, cr *minterv1.CredentialsRequest) error { +func (a *KubevirtActuator) Update(ctx context.Context, cr *minterv1.CredentialsRequest) error { logger := a.getLogger(cr) logger.Debug("running Update") return a.sync(ctx, cr, logger) } // Delete credentials -func (a *OCPActuator) Delete(ctx context.Context, cr *minterv1.CredentialsRequest) error { +func (a *KubevirtActuator) Delete(ctx context.Context, cr *minterv1.CredentialsRequest) error { logger := a.getLogger(cr) logger.Debug("running Delete") @@ -106,11 +106,11 @@ func (a *OCPActuator) Delete(ctx context.Context, cr *minterv1.CredentialsReques } // GetCredentialsRootSecretLocation returns the namespace and name where the parent credentials secret is stored. -func (a *OCPActuator) GetCredentialsRootSecretLocation() types.NamespacedName { +func (a *KubevirtActuator) GetCredentialsRootSecretLocation() types.NamespacedName { return types.NamespacedName{Namespace: constants.CloudCredSecretNamespace, Name: constants.KubevirtCloudCredSecretName} } -func (a *OCPActuator) sync(ctx context.Context, cr *minterv1.CredentialsRequest, logger log.FieldLogger) error { +func (a *KubevirtActuator) sync(ctx context.Context, cr *minterv1.CredentialsRequest, logger log.FieldLogger) error { logger.Debug("running sync") // get the secret data from the credentials request @@ -139,7 +139,7 @@ func (a *OCPActuator) sync(ctx context.Context, cr *minterv1.CredentialsRequest, return nil } -func (a *OCPActuator) getCredentialsSecretData(ctx context.Context, logger log.FieldLogger) ([]byte, error) { +func (a *KubevirtActuator) getCredentialsSecretData(ctx context.Context, logger log.FieldLogger) ([]byte, error) { // get the secret of the kubevirt credentials kubevirtCredentialsSecret := &corev1.Secret{} if err := a.Client.Get(ctx, a.GetCredentialsRootSecretLocation(), kubevirtCredentialsSecret); err != nil { @@ -161,7 +161,7 @@ func (a *OCPActuator) getCredentialsSecretData(ctx context.Context, logger log.F return infraClusterKubeconfig, nil } -func (a *OCPActuator) syncCredentialSecret(ctx context.Context, cr *minterv1.CredentialsRequest, kubevirtCredentialData *[]byte, existingSecret *corev1.Secret, logger log.FieldLogger) error{ +func (a *KubevirtActuator) syncCredentialSecret(ctx context.Context, cr *minterv1.CredentialsRequest, kubevirtCredentialData *[]byte, existingSecret *corev1.Secret, logger log.FieldLogger) error{ if existingSecret == nil { if kubevirtCredentialData == nil { msg := "new access key secret needed but no key data provided" @@ -178,7 +178,7 @@ func (a *OCPActuator) syncCredentialSecret(ctx context.Context, cr *minterv1.Cre return a.updateExistingSecret(logger, existingSecret, cr, kubevirtCredentialData) } -func (a *OCPActuator) updateExistingSecret(logger log.FieldLogger, existingSecret *corev1.Secret, cr *minterv1.CredentialsRequest, kubevirtCredentialData *[]byte) error { +func (a *KubevirtActuator) updateExistingSecret(logger log.FieldLogger, existingSecret *corev1.Secret, cr *minterv1.CredentialsRequest, kubevirtCredentialData *[]byte) error { // Update the existing secret: logger.Debug("updating secret") origSecret := existingSecret.DeepCopy() @@ -209,7 +209,7 @@ func (a *OCPActuator) updateExistingSecret(logger log.FieldLogger, existingSecre return nil } -func (a *OCPActuator) createNewSecret(logger log.FieldLogger, cr *minterv1.CredentialsRequest, kubevirtCredentialData *[]byte, ctx context.Context) error { +func (a *KubevirtActuator) createNewSecret(logger log.FieldLogger, cr *minterv1.CredentialsRequest, kubevirtCredentialData *[]byte, ctx context.Context) error { logger.Info("creating secret") secret := &corev1.Secret{ ObjectMeta: metav1.ObjectMeta{ @@ -233,7 +233,7 @@ func (a *OCPActuator) createNewSecret(logger log.FieldLogger, cr *minterv1.Crede return nil } -func (a *OCPActuator) getSecret(ctx context.Context, cr *minterv1.CredentialsRequest, logger log.FieldLogger) (*corev1.Secret, error) { +func (a *KubevirtActuator) getSecret(ctx context.Context, cr *minterv1.CredentialsRequest, logger log.FieldLogger) (*corev1.Secret, error) { logger.Debug("running getSecret") existingSecret := &corev1.Secret{} @@ -253,15 +253,15 @@ func (a *OCPActuator) getSecret(ctx context.Context, cr *minterv1.CredentialsReq return existingSecret, nil } -func (a *OCPActuator) getLogger(cr *minterv1.CredentialsRequest) log.FieldLogger { +func (a *KubevirtActuator) getLogger(cr *minterv1.CredentialsRequest) log.FieldLogger { return log.WithFields(log.Fields{ - "actuator": "Openshift", + "actuator": "Kubevirt", "targetSecret": fmt.Sprintf("%s/%s", cr.Spec.SecretRef.Namespace, cr.Spec.SecretRef.Name), "cr": fmt.Sprintf("%s/%s", cr.Namespace, cr.Name), }) } -func (a *OCPActuator) Upgradeable(mode operatorv1.CloudCredentialsMode) *configv1.ClusterOperatorStatusCondition { +func (a *KubevirtActuator) Upgradeable(mode operatorv1.CloudCredentialsMode) *configv1.ClusterOperatorStatusCondition { upgradeableCondition := &configv1.ClusterOperatorStatusCondition{ Status: configv1.ConditionTrue, Type: configv1.OperatorUpgradeable, @@ -269,6 +269,6 @@ func (a *OCPActuator) Upgradeable(mode operatorv1.CloudCredentialsMode) *configv return upgradeableCondition } -func (a *OCPActuator) GetUpcomingCredSecrets() []types.NamespacedName { +func (a *KubevirtActuator) GetUpcomingCredSecrets() []types.NamespacedName { return []types.NamespacedName{} } diff --git a/pkg/ocp/actuator_test.go b/pkg/kubevirt/actuator_test.go similarity index 95% rename from pkg/ocp/actuator_test.go rename to pkg/kubevirt/actuator_test.go index 17d5367460..0643999364 100644 --- a/pkg/ocp/actuator_test.go +++ b/pkg/kubevirt/actuator_test.go @@ -14,12 +14,12 @@ See the License for the specific language governing permissions and limitations under the License. */ -package ocp_test +package kubevirt_test import ( "context" "fmt" - "github.com/openshift/cloud-credential-operator/pkg/ocp" + "github.com/openshift/cloud-credential-operator/pkg/kubevirt" kubernetesErrors "k8s.io/apimachinery/pkg/api/errors" "reflect" "testing" @@ -38,7 +38,7 @@ import ( ) const ( - testNamespace = "openshift-cloud-credential-operator" + testNamespace = "Kubevirt-cloud-credential-operator" testCredRequestName = "openshift-machine-api-kubevirt" testInfrastructureName = "test-cluster-abcd" testRandomSuffix = "random" @@ -57,7 +57,7 @@ var ( Namespace: constants.CloudCredSecretNamespace, }, Data: map[string][]byte{ - ocp.KubevirtCredentialsSecretKey: kubevirtCredentialData, + kubevirt.KubevirtCredentialsSecretKey: kubevirtCredentialData, }, } @@ -67,7 +67,7 @@ var ( Namespace: testOpenshiftMachineApiKubevirtNamespace, }, Data: map[string][]byte{ - ocp.KubevirtCredentialsSecretKey: kubevirtCredentialData, + kubevirt.KubevirtCredentialsSecretKey: kubevirtCredentialData, }, } @@ -137,7 +137,7 @@ func TestCreateCR(t *testing.T) { allObjects := append(test.existing, test.credentialsRequest) fakeClient := fake.NewFakeClientWithScheme(scheme.Scheme, allObjects...) - actuator, err := ocp.NewActuator(fakeClient) + actuator, err := kubevirt.NewActuator(fakeClient) if err != nil { assert.Regexp(t, test.errRegexp, err) assert.Nil(t, actuator) @@ -190,7 +190,7 @@ func TestDeleteCR(t *testing.T) { allObjects := append(test.existing, test.credentialsRequest) fakeClient := fake.NewFakeClientWithScheme(scheme.Scheme, allObjects...) - actuator, err := ocp.NewActuator(fakeClient) + actuator, err := kubevirt.NewActuator(fakeClient) if err != nil { assert.Regexp(t, test.errRegexp, err) assert.Nil(t, actuator) @@ -251,7 +251,7 @@ func TestExistsCR(t *testing.T) { allObjects := append(test.existing, test.credentialsRequest) fakeClient := fake.NewFakeClientWithScheme(scheme.Scheme, allObjects...) - actuator, err := ocp.NewActuator(fakeClient) + actuator, err := kubevirt.NewActuator(fakeClient) if err != nil { assert.Regexp(t, test.errRegexp, err) assert.Nil(t, actuator) @@ -316,7 +316,7 @@ func TestUpdateCR(t *testing.T) { allObjects := append(test.existing, test.credentialsRequest) fakeClient := fake.NewFakeClientWithScheme(scheme.Scheme, allObjects...) - actuator, err := ocp.NewActuator(fakeClient) + actuator, err := kubevirt.NewActuator(fakeClient) if err != nil { assert.Regexp(t, test.errRegexp, err) assert.Nil(t, actuator) @@ -374,7 +374,7 @@ func existingObjectsAfterCreate(t *testing.T) []runtime.Object { func testCredentialsRequest(t *testing.T) *minterv1.CredentialsRequest { codec, err := minterv1.NewCodec() if err != nil { - t.Fatalf("error creating OCP codec: %v", err) + t.Fatalf("error creating Kubevirt codec: %v", err) } rawObj, err := codec.EncodeProviderSpec(kubevirtSpec) diff --git a/pkg/operator/controller.go b/pkg/operator/controller.go index 9ba9fb4599..c76beb8ad7 100644 --- a/pkg/operator/controller.go +++ b/pkg/operator/controller.go @@ -31,7 +31,7 @@ import ( "github.com/openshift/cloud-credential-operator/pkg/operator/secretannotator" "github.com/openshift/cloud-credential-operator/pkg/ovirt" "github.com/openshift/cloud-credential-operator/pkg/util" - "github.com/openshift/cloud-credential-operator/pkg/ocp" + "github.com/openshift/cloud-credential-operator/pkg/kubevirt" vsphereactuator "github.com/openshift/cloud-credential-operator/pkg/vsphere/actuator" configv1 "github.com/openshift/api/config/v1" @@ -123,10 +123,9 @@ func AddToManager(m manager.Manager, explicitKubeconfig string) error { if err != nil { return err } - // TODO: change it to OCP case configv1.KubevirtPlatformType: - log.Info("initializing OCP actuator") - a, err = ocp.NewActuator(m.GetClient()) + log.Info("initializing Kubevirt actuator") + a, err = kubevirt.NewActuator(m.GetClient()) if err != nil { return err }