|
| 1 | +package kmm |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/golang/glog" |
| 8 | + "github.com/openshift-kni/eco-goinfra/pkg/clients" |
| 9 | + "github.com/openshift-kni/eco-goinfra/pkg/msg" |
| 10 | + kmmv1beta2 "github.com/openshift-kni/eco-goinfra/pkg/schemes/kmm/v1beta2" |
| 11 | + k8serrors "k8s.io/apimachinery/pkg/api/errors" |
| 12 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 13 | + goclient "sigs.k8s.io/controller-runtime/pkg/client" |
| 14 | +) |
| 15 | + |
| 16 | +// PreflightValidationBuilder provides struct for the preflightvalidation object |
| 17 | +// containing connection to the cluster and the preflightvalidation definitions. |
| 18 | +type PreflightValidationBuilder struct { |
| 19 | + // PreflightValidation definition. Used to create a PreflightValidation object. |
| 20 | + Definition *kmmv1beta2.PreflightValidation |
| 21 | + // Created PreflightValidation object. |
| 22 | + Object *kmmv1beta2.PreflightValidation |
| 23 | + // ApiClient to interact with the cluster. |
| 24 | + apiClient *clients.Settings |
| 25 | + // errorMsg is processed before the object is created or updated. |
| 26 | + errorMsg string |
| 27 | +} |
| 28 | + |
| 29 | +// PreflightValidationAdditionalOptions additional options for preflightvalidation object. |
| 30 | +type PreflightValidationAdditionalOptions func( |
| 31 | + builder *PreflightValidationBuilder) (*PreflightValidationBuilder, error) |
| 32 | + |
| 33 | +// NewPreflightValidationBuilder creates a new instance of PreflightValidationBuilder. |
| 34 | +func NewPreflightValidationBuilder( |
| 35 | + apiClient *clients.Settings, name, nsname string) *PreflightValidationBuilder { |
| 36 | + glog.V(100).Infof("Initializing new PreflightValidation structure with following params: %s, %s", |
| 37 | + name, nsname) |
| 38 | + |
| 39 | + if apiClient == nil { |
| 40 | + glog.V(100).Infof("The apiClient is empty") |
| 41 | + |
| 42 | + return nil |
| 43 | + } |
| 44 | + |
| 45 | + err := apiClient.AttachScheme(kmmv1beta2.AddToScheme) |
| 46 | + if err != nil { |
| 47 | + glog.V(100).Infof("Failed to add kmm v1beta2 scheme to client schemes") |
| 48 | + |
| 49 | + return nil |
| 50 | + } |
| 51 | + |
| 52 | + builder := &PreflightValidationBuilder{ |
| 53 | + apiClient: apiClient, |
| 54 | + Definition: &kmmv1beta2.PreflightValidation{ |
| 55 | + ObjectMeta: metav1.ObjectMeta{ |
| 56 | + Name: name, |
| 57 | + Namespace: nsname, |
| 58 | + }, |
| 59 | + }, |
| 60 | + } |
| 61 | + |
| 62 | + if name == "" { |
| 63 | + glog.V(100).Infof("The name of the PreflightValidation is empty") |
| 64 | + |
| 65 | + builder.errorMsg = "PreflightValidation 'name' cannot be empty" |
| 66 | + |
| 67 | + return builder |
| 68 | + } |
| 69 | + |
| 70 | + if nsname == "" { |
| 71 | + glog.V(100).Infof("The namespace of the PreflightValidation is empty") |
| 72 | + |
| 73 | + builder.errorMsg = "PreflightValidation 'nsname' cannot be empty" |
| 74 | + |
| 75 | + return builder |
| 76 | + } |
| 77 | + |
| 78 | + return builder |
| 79 | +} |
| 80 | + |
| 81 | +// WithKernelVersion sets the kernel for which the preflightvalidation checks the module. |
| 82 | +func (builder *PreflightValidationBuilder) WithKernelVersion(kernelVersion string) *PreflightValidationBuilder { |
| 83 | + if valid, _ := builder.validate(); !valid { |
| 84 | + return builder |
| 85 | + } |
| 86 | + |
| 87 | + if kernelVersion == "" { |
| 88 | + builder.errorMsg = "invalid 'kernelVersion' argument can not be nil" |
| 89 | + |
| 90 | + return builder |
| 91 | + } |
| 92 | + |
| 93 | + glog.V(100).Infof("Creating new PreflightValidation with kernelVersion: %s", |
| 94 | + kernelVersion) |
| 95 | + |
| 96 | + builder.Definition.Spec.KernelVersion = kernelVersion |
| 97 | + |
| 98 | + return builder |
| 99 | +} |
| 100 | + |
| 101 | +// WithPushBuiltImage configures the build to be pushed to the registry. |
| 102 | +func (builder *PreflightValidationBuilder) WithPushBuiltImage(push bool) *PreflightValidationBuilder { |
| 103 | + if valid, _ := builder.validate(); !valid { |
| 104 | + return builder |
| 105 | + } |
| 106 | + |
| 107 | + glog.V(100).Infof("Creating new PreflightValidation with PushBuiltImage set to: %s", push) |
| 108 | + |
| 109 | + builder.Definition.Spec.PushBuiltImage = push |
| 110 | + |
| 111 | + return builder |
| 112 | +} |
| 113 | + |
| 114 | +// WithOptions creates PreflightValidation with generic mutation options. |
| 115 | +func (builder *PreflightValidationBuilder) WithOptions( |
| 116 | + options ...PreflightValidationAdditionalOptions) *PreflightValidationBuilder { |
| 117 | + if valid, _ := builder.validate(); !valid { |
| 118 | + return builder |
| 119 | + } |
| 120 | + |
| 121 | + glog.V(100).Infof("Setting PreflightValidation additional options") |
| 122 | + |
| 123 | + for _, option := range options { |
| 124 | + if option != nil { |
| 125 | + builder, err := option(builder) |
| 126 | + |
| 127 | + if err != nil { |
| 128 | + glog.V(100).Infof("Error occurred in mutation function") |
| 129 | + |
| 130 | + builder.errorMsg = err.Error() |
| 131 | + |
| 132 | + return builder |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + return builder |
| 138 | +} |
| 139 | + |
| 140 | +// PullPreflightValidation fetches existing PreflightValidation from the cluster. |
| 141 | +func PullPreflightValidation(apiClient *clients.Settings, |
| 142 | + name, nsname string) (*PreflightValidationBuilder, error) { |
| 143 | + glog.V(100).Infof("Pulling existing preflightvalidation name % under namespace %s", |
| 144 | + name, nsname) |
| 145 | + |
| 146 | + if apiClient == nil { |
| 147 | + glog.V(100).Infof("The apiClient is empty") |
| 148 | + |
| 149 | + return nil, fmt.Errorf("preflightvalidation 'apiClient' cannot be empty") |
| 150 | + } |
| 151 | + |
| 152 | + err := apiClient.AttachScheme(kmmv1beta2.AddToScheme) |
| 153 | + if err != nil { |
| 154 | + glog.V(100).Infof("Failed to add preflightvalidation v1beta2 scheme to client schemes") |
| 155 | + |
| 156 | + return nil, err |
| 157 | + } |
| 158 | + |
| 159 | + builder := &PreflightValidationBuilder{ |
| 160 | + apiClient: apiClient, |
| 161 | + Definition: &kmmv1beta2.PreflightValidation{ |
| 162 | + ObjectMeta: metav1.ObjectMeta{ |
| 163 | + Name: name, |
| 164 | + Namespace: nsname, |
| 165 | + }, |
| 166 | + }, |
| 167 | + } |
| 168 | + |
| 169 | + if name == "" { |
| 170 | + glog.V(100).Infof("The name of the preflightvalidation is empty") |
| 171 | + |
| 172 | + return builder, fmt.Errorf("%s", "preflightvalidation 'name' cannot be empty") |
| 173 | + } |
| 174 | + |
| 175 | + if nsname == "" { |
| 176 | + glog.V(100).Infof("The namespace of the preflightvalidation is empty") |
| 177 | + |
| 178 | + return builder, fmt.Errorf("%s", "preflightvalidation 'nsname' cannot be empty") |
| 179 | + } |
| 180 | + |
| 181 | + if !builder.Exists() { |
| 182 | + return nil, fmt.Errorf("preflightvalidation object %s doesn't exist in namespace %s", |
| 183 | + name, nsname) |
| 184 | + } |
| 185 | + |
| 186 | + builder.Definition = builder.Object |
| 187 | + |
| 188 | + return builder, nil |
| 189 | +} |
| 190 | + |
| 191 | +// Create builds preflightvalidation in the cluster and stores object in struct. |
| 192 | +func (builder *PreflightValidationBuilder) Create() (*PreflightValidationBuilder, error) { |
| 193 | + if valid, err := builder.validate(); !valid { |
| 194 | + return builder, err |
| 195 | + } |
| 196 | + |
| 197 | + glog.V(100).Infof("Creating preflightvalidation %s in namespace %s", |
| 198 | + builder.Definition.Name, builder.Definition.Namespace) |
| 199 | + |
| 200 | + var err error |
| 201 | + if !builder.Exists() { |
| 202 | + err = builder.apiClient.Create(context.TODO(), builder.Definition) |
| 203 | + if err == nil { |
| 204 | + builder.Object = builder.Definition |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + return builder, err |
| 209 | +} |
| 210 | + |
| 211 | +// Update modifies an existing preflightvalidation on the cluster. |
| 212 | +func (builder *PreflightValidationBuilder) Update() (*PreflightValidationBuilder, error) { |
| 213 | + if valid, err := builder.validate(); !valid { |
| 214 | + return builder, err |
| 215 | + } |
| 216 | + |
| 217 | + glog.V(100).Infof("Updating preflightvalidation %s in namespace %s", |
| 218 | + builder.Definition.Name, builder.Definition.Namespace) |
| 219 | + |
| 220 | + err := builder.apiClient.Update(context.TODO(), builder.Definition) |
| 221 | + |
| 222 | + if err == nil { |
| 223 | + builder.Object = builder.Definition |
| 224 | + } |
| 225 | + |
| 226 | + return builder, err |
| 227 | +} |
| 228 | + |
| 229 | +// Exists checks if the defined preflightvalidation has already been created. |
| 230 | +func (builder *PreflightValidationBuilder) Exists() bool { |
| 231 | + if valid, _ := builder.validate(); !valid { |
| 232 | + return false |
| 233 | + } |
| 234 | + |
| 235 | + glog.V(100).Infof("Checking if preflightvalidation %s exists in namespace %s", |
| 236 | + builder.Definition.Name, builder.Definition.Namespace) |
| 237 | + |
| 238 | + var err error |
| 239 | + builder.Object, err = builder.Get() |
| 240 | + |
| 241 | + return err == nil || !k8serrors.IsNotFound(err) |
| 242 | +} |
| 243 | + |
| 244 | +// Delete removes a preflightvalidation from the cluster. |
| 245 | +func (builder *PreflightValidationBuilder) Delete() (*PreflightValidationBuilder, error) { |
| 246 | + if valid, err := builder.validate(); !valid { |
| 247 | + return builder, err |
| 248 | + } |
| 249 | + |
| 250 | + glog.V(100).Infof("Deleting preflightvalidation %s in namespace %s", |
| 251 | + builder.Definition.Name, builder.Definition.Namespace) |
| 252 | + |
| 253 | + if !builder.Exists() { |
| 254 | + glog.V(100).Infof("preflightvalidation %s namespace %s cannot be deleted because it does not exist", |
| 255 | + builder.Definition.Name, builder.Definition.Namespace) |
| 256 | + |
| 257 | + builder.Object = nil |
| 258 | + |
| 259 | + return builder, nil |
| 260 | + } |
| 261 | + |
| 262 | + err := builder.apiClient.Delete(context.TODO(), builder.Definition) |
| 263 | + |
| 264 | + if err != nil { |
| 265 | + return builder, fmt.Errorf("cannot delete preflightvalidation: %w", err) |
| 266 | + } |
| 267 | + |
| 268 | + builder.Object = nil |
| 269 | + builder.Definition.ResourceVersion = "" |
| 270 | + |
| 271 | + return builder, nil |
| 272 | +} |
| 273 | + |
| 274 | +// Get fetches the defined preflightvalidation from the cluster. |
| 275 | +func (builder *PreflightValidationBuilder) Get() (*kmmv1beta2.PreflightValidation, error) { |
| 276 | + if valid, err := builder.validate(); !valid { |
| 277 | + return nil, err |
| 278 | + } |
| 279 | + |
| 280 | + glog.V(100).Infof("Getting preflightvalidation %s from namespace %s", |
| 281 | + builder.Definition.Name, builder.Definition.Namespace) |
| 282 | + |
| 283 | + preflightvalidation := &kmmv1beta2.PreflightValidation{} |
| 284 | + |
| 285 | + err := builder.apiClient.Get(context.TODO(), goclient.ObjectKey{ |
| 286 | + Name: builder.Definition.Name, |
| 287 | + Namespace: builder.Definition.Namespace, |
| 288 | + }, preflightvalidation) |
| 289 | + |
| 290 | + if err != nil { |
| 291 | + return nil, err |
| 292 | + } |
| 293 | + |
| 294 | + return preflightvalidation, nil |
| 295 | +} |
| 296 | + |
| 297 | +// validate will check that the builder and builder definition are properly initialized before |
| 298 | +// accessing any member fields. |
| 299 | +func (builder *PreflightValidationBuilder) validate() (bool, error) { |
| 300 | + resourceCRD := "PreflightValidation" |
| 301 | + |
| 302 | + if builder == nil { |
| 303 | + glog.V(100).Infof("The %s builder is uninitialized", resourceCRD) |
| 304 | + |
| 305 | + return false, fmt.Errorf("error: received nil %s builder", resourceCRD) |
| 306 | + } |
| 307 | + |
| 308 | + if builder.Definition == nil { |
| 309 | + glog.V(100).Infof("The %s is undefined", resourceCRD) |
| 310 | + |
| 311 | + return false, fmt.Errorf("%s", msg.UndefinedCrdObjectErrString(resourceCRD)) |
| 312 | + } |
| 313 | + |
| 314 | + if builder.apiClient == nil { |
| 315 | + glog.V(100).Infof("The %s builder apiclient is nil", resourceCRD) |
| 316 | + |
| 317 | + return false, fmt.Errorf("%s builder cannot have nil apiClient", resourceCRD) |
| 318 | + } |
| 319 | + |
| 320 | + if builder.errorMsg != "" { |
| 321 | + glog.V(100).Infof("The %s builder has error message: %s", resourceCRD, builder.errorMsg) |
| 322 | + |
| 323 | + return false, fmt.Errorf("%s", builder.errorMsg) |
| 324 | + } |
| 325 | + |
| 326 | + return true, nil |
| 327 | +} |
0 commit comments