forked from nephio-project/nephio
/
reconciler.go
160 lines (142 loc) · 5.7 KB
/
reconciler.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
Copyright 2023 The Nephio Authors.
Licensed 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 specializerreconciler
import (
"context"
"flag"
"fmt"
"github.com/GoogleContainerTools/kpt-functions-sdk/go/fn"
porchv1alpha1 "github.com/GoogleContainerTools/kpt/porch/api/porch/v1alpha1"
"github.com/go-logr/logr"
"github.com/nephio-project/nephio/controllers/pkg/resource"
kptfilelibv1 "github.com/nephio-project/nephio/krm-functions/lib/kptfile/v1"
"github.com/nephio-project/nephio/krm-functions/lib/kptrl"
ipamv1alpha1 "github.com/nokia/k8s-ipam/apis/alloc/ipam/v1alpha1"
vlanv1alpha1 "github.com/nokia/k8s-ipam/apis/alloc/vlan/v1alpha1"
"github.com/pkg/errors"
"github.com/ssharmasandeep/nephio/controllers/pkg/specializer-operator/pkg/controllers/config"
"github.com/ssharmasandeep/nephio/controllers/pkg/specializer-operator/pkg/controllers/ipam"
"github.com/ssharmasandeep/nephio/controllers/pkg/specializer-operator/pkg/controllers/vlan"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime/serializer/json"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
)
type Config struct {
For corev1.ObjectReference
PorchClient client.Client
KRMfunction fn.ResourceListProcessor
}
type Options struct{}
func (o *Options) InitDefaults() {}
func (o *Options) BindFlags(_ string, _ *flag.FlagSet) {}
// SpecializerReconficer reconciles a PackageRevision object
type SpecializerReconciler struct {
client.Client
Options
serializer *json.Serializer
For corev1.ObjectReference
porchClient client.Client
krmfn fn.ResourceListProcessor
l logr.Logger
}
// +kubebuilder:rbac:groups=porch.kpt.dev,resources=packagerevisions,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=porch.kpt.dev,resources=packagerevisions/status,verbs=get;update;patch
// SetupWithManager sets up the controller with the Manager.
func (r *SpecializerReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, i interface{}) error {
if err := vlanv1alpha1.AddToScheme(mgr.GetScheme()); err != nil {
return err
}
if err := ipamv1alpha1.AddToScheme(mgr.GetScheme()); err != nil {
return err
}
if err := porchv1alpha1.AddToScheme(mgr.GetScheme()); err != nil {
return err
}
for _, setup := range []func(context.Context, ctrl.Manager, config.SpecializerControllerConfig) Config{
vlan.Setup,
ipam.Setup,
} {
cfg := setup(ctx, mgr, i.(config.SpecializerControllerConfig))
//ge := make(chan event.GenericEvent)
r.Client = mgr.GetClient()
r.For = cfg.For
r.porchClient = cfg.PorchClient
r.krmfn = cfg.KRMfunction
}
// TBD how does the proxy cache work with the injector for updates
return ctrl.NewControllerManagedBy(mgr).
For(&porchv1alpha1.PackageRevision{}).
Complete(r)
}
func (r *SpecializerReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
r.l = log.FromContext(ctx).WithValues("req", req)
r.l.Info("reconcile specializer")
pr := &porchv1alpha1.PackageRevision{}
if err := r.Get(ctx, req.NamespacedName, pr); err != nil {
// There's no need to requeue if we no longer exist. Otherwise we'll be
// requeued implicitly because we return an error.
if resource.IgnoreNotFound(err) != nil {
r.l.Error(err, "cannot get resource")
return ctrl.Result{}, errors.Wrap(resource.IgnoreNotFound(err), "cannot get resource")
}
return ctrl.Result{}, nil
}
// we just check for forResource conditions and we dont care if it is satisfied already
// this allows us to refresh the allocation.
ct := kptfilelibv1.GetConditionType(&r.For)
if hasSpecificTypeConditions(pr.Status.Conditions, ct) {
// get package revision resourceList
prr := &porchv1alpha1.PackageRevisionResources{}
if err := r.porchClient.Get(ctx, req.NamespacedName, prr); err != nil {
r.l.Error(err, "cannot get package revision resources")
return ctrl.Result{}, errors.Wrap(err, "cannot get package revision resources")
}
// get resourceList from resources
rl, err := kptrl.GetResourceList(prr.Spec.Resources)
if err != nil {
r.l.Error(err, "cannot get resourceList")
return ctrl.Result{}, errors.Wrap(err, "cannot get resourceList")
}
// run the function SDK
_, err = r.krmfn.Process(rl)
if err != nil {
r.l.Error(err, "function run failed")
// TBD if we need to return here + check if kptfile is set
//return ctrl.Result{}, errors.Wrap(err, "function run failed")
}
for _, o := range rl.Items {
r.l.Info("resourceList", "data", o.String())
// TBD what if we create new resources
// update the resources with the latest info
prr.Spec.Resources[o.GetAnnotation(kioutil.PathAnnotation)] = o.String()
}
kptfile := rl.Items.GetRootKptfile()
if kptfile == nil {
r.l.Error(fmt.Errorf("mandatory Kptfile is missing from the package"), "")
return ctrl.Result{}, nil
}
kptf, err := kptfilelibv1.New(rl.Items.GetRootKptfile().String())
if err != nil {
r.l.Error(err, "cannot unmarshal kptfile")
return ctrl.Result{}, nil
}
pr.Status.Conditions = getPorchConditions(kptf.GetConditions())
if err = r.porchClient.Update(ctx, prr); err != nil {
return ctrl.Result{}, err
}
}
return ctrl.Result{}, nil
}