Skip to content

Commit

Permalink
Changing openshift provider to be kubevirt provider
Browse files Browse the repository at this point in the history
  • Loading branch information
bardielle committed Oct 4, 2020
1 parent f4f562b commit 92a0634
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 36 deletions.
44 changes: 22 additions & 22 deletions pkg/ocp/actuator.go → pkg/kubevirt/actuator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -35,31 +35,31 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type OCPActuator struct {
type KubevirtActuator struct {
Client client.Client
Codec *minterv1.ProviderCodec
}

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
}

// 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
Expand All @@ -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")

Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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"
Expand All @@ -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()
Expand Down Expand Up @@ -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{
Expand All @@ -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{}
Expand All @@ -253,22 +253,22 @@ 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,
}
return upgradeableCondition
}

func (a *OCPActuator) GetUpcomingCredSecrets() []types.NamespacedName {
func (a *KubevirtActuator) GetUpcomingCredSecrets() []types.NamespacedName {
return []types.NamespacedName{}
}
20 changes: 10 additions & 10 deletions pkg/ocp/actuator_test.go → pkg/kubevirt/actuator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -57,7 +57,7 @@ var (
Namespace: constants.CloudCredSecretNamespace,
},
Data: map[string][]byte{
ocp.KubevirtCredentialsSecretKey: kubevirtCredentialData,
kubevirt.KubevirtCredentialsSecretKey: kubevirtCredentialData,
},
}

Expand All @@ -67,7 +67,7 @@ var (
Namespace: testOpenshiftMachineApiKubevirtNamespace,
},
Data: map[string][]byte{
ocp.KubevirtCredentialsSecretKey: kubevirtCredentialData,
kubevirt.KubevirtCredentialsSecretKey: kubevirtCredentialData,
},
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 3 additions & 4 deletions pkg/operator/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 92a0634

Please sign in to comment.