-
Notifications
You must be signed in to change notification settings - Fork 27
/
functionmesh_controller.go
121 lines (103 loc) · 3.9 KB
/
functionmesh_controller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package controllers
import (
"context"
"github.com/streamnative/function-mesh/controllers/spec"
"github.com/go-logr/logr"
"github.com/streamnative/function-mesh/api/compute/v1alpha1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
// FunctionMeshReconciler reconciles a FunctionMesh object
type FunctionMeshReconciler struct {
client.Client
Log logr.Logger
Scheme *runtime.Scheme
}
// +kubebuilder:rbac:groups=compute.functionmesh.io,resources=functionmeshes,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=compute.functionmesh.io,resources=functionmeshes/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=coordination.k8s.io,resources=leases,verbs=get;list;create;update;delete
func (r *FunctionMeshReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
_ = r.Log.WithValues("functionMesh", req.NamespacedName)
// your logic here
mesh := &v1alpha1.FunctionMesh{}
err := r.Get(ctx, req.NamespacedName, mesh)
if err != nil {
if errors.IsNotFound(err) {
// mesh must be deleted
return ctrl.Result{}, nil
}
r.Log.Error(err, "failed to get mesh")
return reconcile.Result{Requeue: true}, err
}
if !spec.IsManaged(mesh) {
r.Log.Info("Skipping function mesh not managed by the controller", "Name", req.String())
return reconcile.Result{}, nil
}
// initialize component status map
if mesh.Status.FunctionConditions == nil {
mesh.Status.FunctionConditions = make(map[string]v1alpha1.ResourceCondition)
}
if mesh.Status.SourceConditions == nil {
mesh.Status.SourceConditions = make(map[string]v1alpha1.ResourceCondition)
}
if mesh.Status.SinkConditions == nil {
mesh.Status.SinkConditions = make(map[string]v1alpha1.ResourceCondition)
}
if mesh.Status.Condition == nil {
mesh.Status.Condition = &v1alpha1.ResourceCondition{}
}
// TODO validate function spec correctness such as no duplicated func name etc
// make observations
err = r.ObserveFunctionMesh(ctx, req, mesh)
if err != nil {
return reconcile.Result{}, err
}
err = r.Status().Update(ctx, mesh)
if err != nil {
r.Log.Error(err, "failed to update mesh status")
return ctrl.Result{}, err
}
isNewGeneration := r.checkIfFunctionMeshGenerationsIsIncreased(mesh)
// apply changes
err = r.UpdateFunctionMesh(ctx, req, mesh, isNewGeneration)
if err != nil {
return reconcile.Result{}, err
}
mesh.Status.ObservedGeneration = mesh.Generation
err = r.Status().Update(ctx, mesh)
if err != nil {
r.Log.Error(err, "failed to update functionmesh status")
return ctrl.Result{}, err
}
return ctrl.Result{}, nil
}
func (r *FunctionMeshReconciler) checkIfFunctionMeshGenerationsIsIncreased(mesh *v1alpha1.FunctionMesh) bool {
return mesh.Generation != mesh.Status.ObservedGeneration
}
func (r *FunctionMeshReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&v1alpha1.FunctionMesh{}).
Owns(&v1alpha1.Function{}).
Owns(&v1alpha1.Source{}).
Owns(&v1alpha1.Sink{}).
Complete(r)
}