2 * ============LICENSE_START=======================================================
3 * oom-certservice-k8s-external-provider
4 * ================================================================================
5 * Copyright (c) 2019 Smallstep Labs, Inc.
6 * Modifications copyright (C) 2020 Nokia. All rights reserved.
7 * ================================================================================
8 * This source code was copied from the following git repository:
9 * https://github.com/smallstep/step-issuer
10 * The source code was modified for usage in the ONAP project.
11 * ================================================================================
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
16 * http://www.apache.org/licenses/LICENSE-2.0
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 * ============LICENSE_END=========================================================
26 package cmpv2controller
32 "github.com/go-logr/logr"
33 core "k8s.io/api/core/v1"
34 apierrors "k8s.io/apimachinery/pkg/api/errors"
35 "k8s.io/apimachinery/pkg/runtime"
36 "k8s.io/apimachinery/pkg/types"
37 "k8s.io/client-go/tools/record"
39 ctrl "sigs.k8s.io/controller-runtime"
40 "sigs.k8s.io/controller-runtime/pkg/client"
42 "onap.org/oom-certservice/k8s-external-provider/src/cmpv2api"
43 provisioners "onap.org/oom-certservice/k8s-external-provider/src/cmpv2provisioner"
46 // CMPv2IssuerController reconciles a CMPv2Issuer object
47 type CMPv2IssuerController struct {
51 Recorder record.EventRecorder
54 // Reconcile will read and validate the CMPv2Issuer resources, it will set the
55 // status condition ready to true if everything is right.
56 func (controller *CMPv2IssuerController) Reconcile(req ctrl.Request) (ctrl.Result, error) {
57 ctx := context.Background()
58 log := controller.Log.WithValues("cmpv2-issuer-controller", req.NamespacedName)
60 // 1. Load CMPv2Issuer
61 issuer := new(cmpv2api.CMPv2Issuer)
62 if err := controller.loadResource(ctx, req.NamespacedName, issuer); err != nil {
63 handleErrorLoadingCMPv2Issuer(log, err)
64 return ctrl.Result{}, client.IgnoreNotFound(err)
66 log.Info("CMPv2Issuer loaded: ", "issuer", issuer)
68 // 2. Validate CMPv2Issuer
69 statusUpdater := newStatusUpdater(controller, issuer, log)
70 if err := validateCMPv2IssuerSpec(issuer.Spec, log); err != nil {
71 handleErrorCMPv2IssuerValidation(ctx, log, err, statusUpdater)
72 return ctrl.Result{}, err
75 // 3. Load keystore and truststore information form k8s secret
76 var secret core.Secret
77 secretNamespaceName := types.NamespacedName{
78 Namespace: req.Namespace,
79 Name: issuer.Spec.CertSecretRef.Name,
81 if err := controller.loadResource(ctx, secretNamespaceName, &secret); err != nil {
82 handleErrorInvalidSecret(ctx, log, err, statusUpdater, secretNamespaceName)
83 return ctrl.Result{}, err
86 // 4. Create CMPv2 provisioner and store the instance for further use
87 provisioner, err := provisioners.CreateProvisioner(issuer, secret)
89 log.Error(err, "failed to initialize provisioner")
90 statusUpdater.UpdateNoError(ctx, cmpv2api.ConditionFalse, "Error", "Failed to initialize provisioner: %v", err)
91 handleErrorProvisionerInitialization(ctx, log, err, statusUpdater)
92 return ctrl.Result{}, err
94 provisioners.Store(req.NamespacedName, provisioner)
96 // 5. Update the status of CMPv2Issuer to 'Validated'
97 if err := updateCMPv2IssuerStatusToVerified(statusUpdater, ctx, log); err != nil {
98 handleErrorUpdatingCMPv2IssuerStatus(log, err)
99 return ctrl.Result{}, err
102 return ctrl.Result{}, nil
105 func (controller *CMPv2IssuerController) SetupWithManager(manager ctrl.Manager) error {
106 return ctrl.NewControllerManagedBy(manager).
107 For(&cmpv2api.CMPv2Issuer{}).
111 func (controller *CMPv2IssuerController) loadResource(ctx context.Context, key client.ObjectKey, obj runtime.Object) error {
112 return controller.Client.Get(ctx, key, obj)
115 func validateCMPv2IssuerSpec(issuerSpec cmpv2api.CMPv2IssuerSpec, log logr.Logger) error {
117 case issuerSpec.URL == "":
118 return fmt.Errorf("spec.url cannot be empty")
119 case issuerSpec.CaName == "":
120 return fmt.Errorf("spec.caName cannot be empty")
121 case issuerSpec.CertSecretRef.Name == "":
122 return fmt.Errorf("spec.certSecretRef.name cannot be empty")
123 case issuerSpec.CertSecretRef.KeyRef == "":
124 return fmt.Errorf("spec.certSecretRef.keyRef cannot be empty")
125 case issuerSpec.CertSecretRef.CertRef == "":
126 return fmt.Errorf("spec.certSecretRef.certRef cannot be empty")
127 case issuerSpec.CertSecretRef.CacertRef == "":
128 return fmt.Errorf("spec.certSecretRef.cacertRef cannot be empty")
134 func updateCMPv2IssuerStatusToVerified(statusUpdater *CMPv2IssuerStatusUpdater, ctx context.Context, log logr.Logger) error {
135 log.Info("CMPv2 provisioner created -> updating status to of CMPv2Issuer resource to: Verified")
136 return statusUpdater.Update(ctx, cmpv2api.ConditionTrue, Verified, "CMPv2Issuer verified and ready to sign certificates")
141 func handleErrorUpdatingCMPv2IssuerStatus(log logr.Logger, err error) {
142 log.Error(err, "Failed to update CMPv2Issuer status")
145 func handleErrorLoadingCMPv2Issuer(log logr.Logger, err error) {
146 log.Error(err, "Failed to retrieve CMPv2Issuer resource")
149 func handleErrorProvisionerInitialization(ctx context.Context, log logr.Logger, err error, statusUpdater *CMPv2IssuerStatusUpdater) {
150 log.Error(err, "Failed to initialize provisioner")
151 statusUpdater.UpdateNoError(ctx, cmpv2api.ConditionFalse, Error, "Failed to initialize provisioner: %v", err)
154 func handleErrorCMPv2IssuerValidation(ctx context.Context, log logr.Logger, err error, statusUpdater *CMPv2IssuerStatusUpdater) {
155 log.Error(err, "Failed to validate CMPv2Issuer resource")
156 statusUpdater.UpdateNoError(ctx, cmpv2api.ConditionFalse, ValidationFailed, "Failed to validate resource: %v", err)
159 func handleErrorInvalidSecret(ctx context.Context, log logr.Logger, err error, statusUpdater *CMPv2IssuerStatusUpdater, secretNamespaceName types.NamespacedName) {
160 log.Error(err, "Failed to retrieve CMPv2Issuer provisioner secret", "namespace", secretNamespaceName.Namespace, "name", secretNamespaceName.Name)
161 if apierrors.IsNotFound(err) {
162 statusUpdater.UpdateNoError(ctx, cmpv2api.ConditionFalse, NotFound, "Failed to retrieve provisioner secret: %v", err)
164 statusUpdater.UpdateNoError(ctx, cmpv2api.ConditionFalse, Error, "Failed to retrieve provisioner secret: %v", err)