-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
creation of onboarding job be part seaprate ensure function
Signed-off-by: rchikatw <[email protected]>
- Loading branch information
Showing
5 changed files
with
115 additions
and
75 deletions.
There are no files selected for viewing
111 changes: 111 additions & 0 deletions
111
controllers/storagecluster/onboarding_validation_keys_generator_job.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,111 @@ | ||
package storagecluster | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
batchv1 "k8s.io/api/batch/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/utils/ptr" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
|
||
ocsv1 "github.com/red-hat-storage/ocs-operator/api/v4/v1" | ||
"github.com/red-hat-storage/ocs-operator/v4/controllers/util" | ||
"k8s.io/klog/v2" | ||
controllerutil "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" | ||
) | ||
|
||
const ( | ||
onboardingValidationKeysGeneratorImage = "ONBOARDING_VALIDATION_KEYS_GENERATOR_IMAGE" | ||
onboardingValidationKeysGeneratorJobName = "onboarding-validation-keys-generator" | ||
onboardingValidationPublicKeySecretName = "onboarding-ticket-key" | ||
) | ||
|
||
var onboardingJob = &batchv1.Job{ | ||
Spec: batchv1.JobSpec{ | ||
// Eligible to delete automatically when job finishes | ||
TTLSecondsAfterFinished: ptr.To(int32(0)), | ||
Template: corev1.PodTemplateSpec{ | ||
Spec: corev1.PodSpec{ | ||
RestartPolicy: corev1.RestartPolicyOnFailure, | ||
ServiceAccountName: onboardingValidationKeysGeneratorJobName, | ||
Containers: []corev1.Container{ | ||
{ | ||
Name: onboardingValidationKeysGeneratorJobName, | ||
Image: os.Getenv(onboardingValidationKeysGeneratorImage), | ||
Command: []string{"/usr/local/bin/onboarding-validation-keys-gen"}, | ||
Env: []corev1.EnvVar{ | ||
{ | ||
Name: util.OperatorNamespaceEnvVar, | ||
Value: os.Getenv(util.OperatorNamespaceEnvVar), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
type onboardingValidationKeysGeneratorJob struct{} | ||
|
||
var _ resourceManager = &onboardingValidationKeysGeneratorJob{} | ||
|
||
func (o *onboardingValidationKeysGeneratorJob) ensureCreated(r *StorageClusterReconciler, instance *ocsv1.StorageCluster) (reconcile.Result, error) { | ||
|
||
if !instance.Spec.AllowRemoteStorageConsumers { | ||
r.Log.Info("Spec.AllowRemoteStorageConsumers is disabled, skipping onboarding validation key generator job creation") | ||
return reconcile.Result{}, nil | ||
} | ||
|
||
if res, err := o.createJob(r, instance); err != nil { | ||
return reconcile.Result{}, err | ||
} else if !res.IsZero() { | ||
return res, nil | ||
} | ||
|
||
return reconcile.Result{}, nil | ||
} | ||
|
||
func (o *onboardingValidationKeysGeneratorJob) createJob(r *StorageClusterReconciler, instance *ocsv1.StorageCluster) (reconcile.Result, error) { | ||
var err error | ||
r.Log.Info("Spec.AllowRemoteStorageConsumers is enabled. Creating Onboarding validation key generator job") | ||
if os.Getenv(onboardingValidationKeysGeneratorImage) == "" { | ||
err = fmt.Errorf("OnboardingSecretGeneratorImage env var is not set") | ||
r.Log.Error(err, "No value set for env variable") | ||
return reconcile.Result{}, err | ||
} | ||
publicKeySecret := &corev1.Secret{} | ||
publicKeySecret.Name = onboardingValidationPublicKeySecretName | ||
actualSecret := &corev1.Secret{} | ||
actualSecret.Namespace = instance.Namespace | ||
// Creating the job only if public is not found | ||
err = r.Client.Get(r.ctx, client.ObjectKeyFromObject(publicKeySecret), actualSecret) | ||
if errors.IsNotFound(err) { | ||
onboardingSecretGeneratorJob := onboardingJob.DeepCopy() | ||
onboardingSecretGeneratorJob.Name = onboardingValidationKeysGeneratorJobName | ||
onboardingSecretGeneratorJob.Namespace = instance.Namespace | ||
_, err := controllerutil.CreateOrUpdate(r.ctx, r.Client, onboardingSecretGeneratorJob, nil) | ||
if err != nil { | ||
r.Log.Error(err, "failed to create onboarding validation key generator job") | ||
return reconcile.Result{}, err | ||
} | ||
|
||
} | ||
return reconcile.Result{}, nil | ||
} | ||
|
||
func (o *onboardingValidationKeysGeneratorJob) ensureDeleted(r *StorageClusterReconciler, instance *ocsv1.StorageCluster) (reconcile.Result, error) { | ||
onboardingSecretGeneratorJob := &batchv1.Job{} | ||
onboardingSecretGeneratorJob.Name = onboardingValidationKeysGeneratorJobName | ||
onboardingSecretGeneratorJob.Namespace = instance.Namespace | ||
err := r.Client.Delete(r.ctx, onboardingSecretGeneratorJob) | ||
if err != nil && !errors.IsNotFound(err) { | ||
klog.Infof("Failed to delete secret %s: %v", onboardingSecretGeneratorJob.Name, err) | ||
return reconcile.Result{}, err | ||
} | ||
|
||
return reconcile.Result{}, nil | ||
} |
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