|
| 1 | +/* |
| 2 | +Copyright 2019 The Knative Authors |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package crds |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "errors" |
| 22 | + "fmt" |
| 23 | + |
| 24 | + "go.uber.org/zap" |
| 25 | + corev1 "k8s.io/api/core/v1" |
| 26 | + apiextv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1" |
| 27 | + |
| 28 | + apiextclientset "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" |
| 29 | + apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/client/listers/apiextensions/v1beta1" |
| 30 | + "k8s.io/apimachinery/pkg/api/equality" |
| 31 | + apierrs "k8s.io/apimachinery/pkg/api/errors" |
| 32 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 33 | + "k8s.io/client-go/kubernetes" |
| 34 | + "k8s.io/client-go/tools/record" |
| 35 | + "knative.dev/pkg/controller" |
| 36 | + "knative.dev/pkg/logging" |
| 37 | + "knative.dev/pkg/system" |
| 38 | + servingv1beta1 "knative.dev/serving/pkg/apis/serving/v1beta1" |
| 39 | + servingclient "knative.dev/serving/pkg/client/clientset/versioned" |
| 40 | + servingv1beta1listers "knative.dev/serving/pkg/client/listers/serving/v1beta1" |
| 41 | + |
| 42 | + "github.com/lionelvillard/knative-functions-controller/pkg/reconciler/crds/resources" |
| 43 | +) |
| 44 | + |
| 45 | +// Reconciler implements controller.Reconciler for dynamic resources. |
| 46 | +type Reconciler struct { |
| 47 | + // KubeClient allows us to talk to the k8s for core APIs |
| 48 | + kubeClient kubernetes.Interface |
| 49 | + |
| 50 | + // |
| 51 | + crdClient apiextclientset.Interface |
| 52 | + |
| 53 | + // servingClient allows us to talk to the serving APIs |
| 54 | + servingClient servingclient.Interface |
| 55 | + |
| 56 | + // serviceLister index properties about Knative services |
| 57 | + serviceLister servingv1beta1listers.ServiceLister |
| 58 | + |
| 59 | + // crdLister index properties about CRDs |
| 60 | + crdLister apiextensionsv1beta1.CustomResourceDefinitionLister |
| 61 | + |
| 62 | + // Recorder is an event recorder for recording Event resources to the |
| 63 | + // Kubernetes API. |
| 64 | + Recorder record.EventRecorder |
| 65 | +} |
| 66 | + |
| 67 | +// Check that our Reconciler implements controller.Reconciler |
| 68 | +var _ controller.Reconciler = (*Reconciler)(nil) |
| 69 | + |
| 70 | +// Reconcile implements controller.Reconciler |
| 71 | +func (r *Reconciler) Reconcile(ctx context.Context, name string) error { |
| 72 | + logger := logging.FromContext(ctx) |
| 73 | + |
| 74 | + crd, err := r.crdLister.Get(name) |
| 75 | + if apierrs.IsNotFound(err) { |
| 76 | + // The resource may no longer exist, in which case we stop processing. |
| 77 | + logger.Errorf("resource %q no longer exists", name) |
| 78 | + return nil |
| 79 | + } else if err != nil { |
| 80 | + return err |
| 81 | + } |
| 82 | + |
| 83 | + return r.reconcile(ctx, crd) |
| 84 | +} |
| 85 | + |
| 86 | +func (r *Reconciler) reconcile(ctx context.Context, crd *apiextv1beta1.CustomResourceDefinition) error { |
| 87 | + if crd.GetDeletionTimestamp() != nil { |
| 88 | + // Check for a DeletionTimestamp. If present, elide the normal reconcile logic. |
| 89 | + // When a controller needs finalizer handling, it would go here. |
| 90 | + return nil |
| 91 | + } |
| 92 | + |
| 93 | + functionName := crd.Spec.Names.Plural |
| 94 | + |
| 95 | + // Make sure the function service/configmaps exists |
| 96 | + cm, err := r.reconcileConfig(ctx, functionName) |
| 97 | + if err != nil { |
| 98 | + return err |
| 99 | + } |
| 100 | + |
| 101 | + _, err = r.reconcileService(ctx, functionName, cm) |
| 102 | + if err != nil { |
| 103 | + |
| 104 | + return err |
| 105 | + } |
| 106 | + |
| 107 | + return nil |
| 108 | +} |
| 109 | + |
| 110 | +func (r *Reconciler) reconcileConfig(ctx context.Context, functionName string) (*corev1.ConfigMap, error) { |
| 111 | + logger := logging.FromContext(ctx) |
| 112 | + |
| 113 | + cmname := fmt.Sprintf("config-function-%s", functionName) |
| 114 | + |
| 115 | + cm, err := r.kubeClient.CoreV1().ConfigMaps(system.Namespace()).Get(cmname, metav1.GetOptions{}) |
| 116 | + if err != nil { |
| 117 | + if apierrs.IsNotFound(err) { |
| 118 | + cm, err = resources.MakeConfigMap(system.Namespace(), cmname) |
| 119 | + if err != nil { |
| 120 | + logger.Error("Failed to create the function configmap", zap.Error(err)) |
| 121 | + return nil, err |
| 122 | + } |
| 123 | + cm, err = r.kubeClient.CoreV1().ConfigMaps(system.Namespace()).Create(cm) |
| 124 | + if err != nil { |
| 125 | + logger.Error("Failed to create the function configmap", zap.Error(err)) |
| 126 | + return nil, err |
| 127 | + } |
| 128 | + } else { |
| 129 | + logger.Error("Unable to get the function configmap", zap.Error(err)) |
| 130 | + return nil, err |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + return cm, nil |
| 135 | +} |
| 136 | + |
| 137 | +func (r *Reconciler) reconcileService(ctx context.Context, functionName string, cm *corev1.ConfigMap) (*servingv1beta1.Service, error) { |
| 138 | + logger := logging.FromContext(ctx) |
| 139 | + |
| 140 | + crd, err := r.crdLister.Get(functionName + ".functions.knative.dev") |
| 141 | + if err != nil { |
| 142 | + logger.Error("Failed to get function Custom Resource Definition", zap.Error(err)) |
| 143 | + return nil, fmt.Errorf("Failed to get function Custom Resource Definition: %v", err) |
| 144 | + } |
| 145 | + |
| 146 | + image, ok := crd.Annotations["functions.knative.dev/image"] |
| 147 | + if !ok { |
| 148 | + logger.Error("Missing functions.knative.dev/image annotation on function CRD", zap.Any("functionName", functionName)) |
| 149 | + return nil, errors.New("Missing functions.knative.dev/image annotation on function CRD") |
| 150 | + } |
| 151 | + |
| 152 | + expected := resources.MakeKnativeService(functionName, cm.ResourceVersion, image) |
| 153 | + |
| 154 | + // Update service annotation with config map UUID. |
| 155 | + service, err := r.serviceLister.Services("knative-functions").Get(functionName) |
| 156 | + if err != nil { |
| 157 | + if apierrs.IsNotFound(err) { |
| 158 | + |
| 159 | + ksvc, err := r.servingClient.ServingV1beta1().Services("knative-functions").Create(expected) |
| 160 | + if err != nil { |
| 161 | + logger.Error("Failed to create the function service", zap.Error(err)) |
| 162 | + return nil, fmt.Errorf("Failed to create the function service: %v", err) |
| 163 | + } |
| 164 | + return ksvc, nil |
| 165 | + } |
| 166 | + |
| 167 | + logger.Error("Unable to get the function service", zap.Error(err)) |
| 168 | + return nil, err |
| 169 | + } else if !equality.Semantic.DeepEqual(expected.Spec, service.Spec) { |
| 170 | + service = service.DeepCopy() |
| 171 | + service.Spec = expected.Spec |
| 172 | + |
| 173 | + service, err = r.servingClient.ServingV1beta1().Services("knative-functions").Update(service) |
| 174 | + if err != nil { |
| 175 | + logger.Error("Failed to update the function service", zap.Error(err)) |
| 176 | + return nil, fmt.Errorf("Failed to update the function service: %v", err) |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + return service, nil |
| 181 | +} |
0 commit comments