/
util.go
91 lines (79 loc) · 2.98 KB
/
util.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
/*
Copyright 2020 The Kubernetes 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 util
import (
"context"
"github.com/go-logr/logr"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
clusterv1exp "sigs.k8s.io/cluster-api/exp/api/v1alpha3"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
// GetOwnerMachinePool returns the MachinePool objects owning the current resource.
func GetOwnerMachinePool(ctx context.Context, c client.Client, obj metav1.ObjectMeta) (*clusterv1exp.MachinePool, error) {
for _, ref := range obj.OwnerReferences {
if ref.Kind != "MachinePool" {
continue
}
gv, err := schema.ParseGroupVersion(ref.APIVersion)
if err != nil {
return nil, errors.WithStack(err)
}
if gv.Group == clusterv1exp.GroupVersion.Group {
return GetMachinePoolByName(ctx, c, obj.Namespace, ref.Name)
}
}
return nil, nil
}
// GetMachinePoolByName finds and returns a MachinePool object usting the specified params.
func GetMachinePoolByName(ctx context.Context, c client.Client, namespace, name string) (*clusterv1exp.MachinePool, error) {
m := &clusterv1exp.MachinePool{}
key := client.ObjectKey{Name: name, Namespace: namespace}
if err := c.Get(ctx, key, m); err != nil {
return nil, err
}
return m, nil
}
// MachinePoolToInfrastructureMapFunc returns a handler.ToRequestsFunc that watches for
// MachinePool events and returns reconciliation requests for an infrastructure provider object.
func MachinePoolToInfrastructureMapFunc(gvk schema.GroupVersionKind, log logr.Logger) handler.ToRequestsFunc {
log = log.WithValues("machine-pool-to-infra-map-func", gvk.String())
return func(o handler.MapObject) []reconcile.Request {
log := log.WithValues("namespace", o.Meta.GetNamespace(), "name", o.Meta.GetName())
m, ok := o.Object.(*clusterv1exp.MachinePool)
if !ok {
log.V(4).Info("not a machine pool")
return nil
}
gk := gvk.GroupKind()
ref := m.Spec.Template.Spec.InfrastructureRef
// Return early if the GroupKind doesn't match what we expect.
infraGK := ref.GroupVersionKind().GroupKind()
if gk != infraGK {
log.V(4).Info("infra kind doesn't match filter group kind", infraGK.String())
return nil
}
log.V(4).Info("projecting object", "namespace", m.Namespace, "name", ref.Name)
return []reconcile.Request{
{
NamespacedName: client.ObjectKey{
Namespace: m.Namespace,
Name: ref.Name,
},
},
}
}
}