Skip to content

Commit a78b1ef

Browse files
authored
improve run CR's create validation (#22)
This improvement adds the following validations: **ConfigMap field:** - checks that "configMapName" field is not empty. - checks that the configMap exists. - check that the configMap has "data.tnf_config.yaml" field, whose value is not empty. **Secret field:** changed to *string (optional) in the CRD spec. Validation checks: - If it's nil, return. - If it's not nil: - Check that it's not an empty string. - Check that the secret exists. - Check that the secret has "data.preflight_dockerconfig.json" field, whose value is not empty.
1 parent edfaf41 commit a78b1ef

File tree

5 files changed

+64
-16
lines changed

5 files changed

+64
-16
lines changed

api/v1alpha1/cnfcertificationsuiterun_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ type CnfCertificationSuiteRunSpec struct {
3838
// ConfigMapName holds the cnf certification suite yaml config.
3939
ConfigMapName string `json:"configMapName"`
4040
// PreflightSecretName holds the secret name for preflight's dockerconfig.
41-
PreflightSecretName string `json:"preflightSecretName"`
41+
PreflightSecretName *string `json:"preflightSecretName,omitempty"`
4242
}
4343

4444
// CnfCertificationSuiteRunStatus defines the observed state of CnfCertificationSuiteRun

api/v1alpha1/cnfcertificationsuiterun_webhook.go

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ var (
4040
configMapLoggerKey = "configMapName"
4141
preflightSecretLoggerKey = "preflightSecretName"
4242
logLevelLoggerKey = "logLevel"
43+
namespaceLoggerKey = "ns"
44+
cnfCertSuiteRunLoggerKey = "cnfCertificationSuiteRun"
4345
)
4446

4547
func (r *CnfCertificationSuiteRun) SetupWebhookWithManager(mgr ctrl.Manager) error {
@@ -97,25 +99,60 @@ func (r *CnfCertificationSuiteRun) ValidateCreate() error {
9799

98100
func (r *CnfCertificationSuiteRun) validateConfigMap() error {
99101
configMap := &v1.ConfigMap{}
102+
103+
if r.Spec.ConfigMapName == "" {
104+
err := fmt.Errorf("spec.configMapName must not be an empty string")
105+
logger.Error(err, "CnfCertificationSuiteRun's config map name is invalid",
106+
configMapLoggerKey, r.Spec.ConfigMapName, namespaceLoggerKey, r.Namespace)
107+
return err
108+
}
109+
110+
// Return an error if config map is not found by name and ns, or field is empty
100111
err := c.Get(context.TODO(), types.NamespacedName{Name: r.Spec.ConfigMapName, Namespace: r.Namespace}, configMap)
101112
if err != nil {
102113
logger.Error(err, "CnfCertificationSuiteRun's config map name field is invalid",
103-
configMapLoggerKey, r.Spec.ConfigMapName)
114+
configMapLoggerKey, r.Spec.ConfigMapName, namespaceLoggerKey, r.Namespace)
104115
return err
105116
}
106-
logger.Info("CnfCertificationSuiteRun's config map name field is valid", configMapLoggerKey, configMap.Name)
107-
return err
117+
118+
// Verify required field exists and that it's not empty
119+
if value, exists := configMap.Data["tnf_config.yaml"]; !exists || value == "" {
120+
err := fmt.Errorf("config map's 'tnf_config.yaml' field must be set with a non-empty and valid configuration yaml for the CNF Certification Suite")
121+
logger.Error(err, "CnfCertificationSuiteRun's config map is invalid",
122+
configMapLoggerKey, r.Spec.ConfigMapName, namespaceLoggerKey, r.Namespace)
123+
return err
124+
}
125+
126+
logger.Info("CnfCertificationSuiteRun's config map field is valid", configMapLoggerKey, configMap.Name, namespaceLoggerKey, r.Namespace)
127+
return nil
108128
}
109129

110130
func (r *CnfCertificationSuiteRun) validatePreflightSecret() error {
111131
preflightSecret := &v1.Secret{}
112-
err := c.Get(context.TODO(), types.NamespacedName{Name: r.Spec.PreflightSecretName, Namespace: r.Namespace}, preflightSecret)
132+
133+
// Nil Preflight Secret is valid
134+
if r.Spec.PreflightSecretName == nil {
135+
logger.Info("Warning: No preflight secret was set.", cnfCertSuiteRunLoggerKey, r.Name, namespaceLoggerKey, r.Namespace)
136+
return nil
137+
}
138+
139+
// Return an error if preflight secret is not found by name and ns, or field is empty
140+
err := c.Get(context.TODO(), types.NamespacedName{Name: *r.Spec.PreflightSecretName, Namespace: r.Namespace}, preflightSecret)
113141
if err != nil {
114142
logger.Error(err, "CnfCertificationSuiteRun's preflight secret name field is invalid",
115-
preflightSecretLoggerKey, r.Spec.PreflightSecretName)
143+
preflightSecretLoggerKey, r.Spec.PreflightSecretName, namespaceLoggerKey, r.Namespace)
116144
return err
117145
}
118-
logger.Info("CnfCertificationSuiteRun's preflight secret name field is valid", preflightSecretLoggerKey, preflightSecret.Name)
146+
147+
// Verify required field exists and that it's not empty
148+
if value, exists := preflightSecret.Data["preflight_dockerconfig.json"]; !exists || value == nil {
149+
err := fmt.Errorf("preflight secret's 'preflight_dockerconfig.json' field must be set with a valid docker config json content")
150+
logger.Error(err, "CnfCertificationSuiteRun's preflight secret is invalid",
151+
configMapLoggerKey, r.Spec.ConfigMapName, namespaceLoggerKey, r.Namespace)
152+
return err
153+
}
154+
155+
logger.Info("CnfCertificationSuiteRun's preflight secret field is valid", preflightSecretLoggerKey, preflightSecret.Name, namespaceLoggerKey, r.Namespace)
119156
return nil
120157
}
121158

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/cnf-certifications.redhat.com_cnfcertificationsuiteruns.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ spec:
6767
- configMapName
6868
- labelsFilter
6969
- logLevel
70-
- preflightSecretName
7170
- timeout
7271
type: object
7372
status:

controllers/cnf-cert-job/cnfcertjob.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,6 @@ func newInitialJobPod() *corev1.Pod {
100100
ReadOnly: true,
101101
MountPath: definitions.CnfCnfCertSuiteConfigFolder,
102102
},
103-
{
104-
Name: "cnf-certsuite-preflight-dockerconfig",
105-
ReadOnly: true,
106-
MountPath: definitions.CnfPreflightConfigFolder,
107-
},
108103
},
109104
},
110105
},
@@ -199,17 +194,29 @@ func WithConfigMap(configMapName string) func(*corev1.Pod) error {
199194
}
200195
}
201196

202-
func WithPreflightSecret(preflightSecretName string) func(*corev1.Pod) error {
197+
func WithPreflightSecret(preflightSecretName *string) func(*corev1.Pod) error {
203198
return func(p *corev1.Pod) error {
199+
if preflightSecretName == nil {
200+
return nil
201+
}
202+
204203
Volume := corev1.Volume{
205204
Name: "cnf-certsuite-preflight-dockerconfig",
206205
VolumeSource: corev1.VolumeSource{
207206
Secret: &corev1.SecretVolumeSource{
208-
SecretName: preflightSecretName,
207+
SecretName: *preflightSecretName,
209208
},
210209
},
211210
}
212211
p.Spec.Volumes = append(p.Spec.Volumes, Volume)
212+
213+
cnfCertCuiteContainer := getCnfCertSuiteContainer(p)
214+
volumeMount := corev1.VolumeMount{
215+
Name: "cnf-certsuite-preflight-dockerconfig",
216+
ReadOnly: true,
217+
MountPath: definitions.CnfPreflightConfigFolder,
218+
}
219+
cnfCertCuiteContainer.VolumeMounts = append(cnfCertCuiteContainer.VolumeMounts, volumeMount)
213220
return nil
214221
}
215222
}

0 commit comments

Comments
 (0)