forked from kubernetes-sigs/cluster-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
107 lines (93 loc) · 3.58 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
Copyright 2019 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 external
import (
"context"
"fmt"
"strings"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// Get uses the client and reference to get an external, unstructured object.
func Get(c client.Client, ref *corev1.ObjectReference, namespace string) (*unstructured.Unstructured, error) {
obj := new(unstructured.Unstructured)
obj.SetAPIVersion(ref.APIVersion)
obj.SetKind(ref.Kind)
obj.SetName(ref.Name)
key := client.ObjectKey{Name: obj.GetName(), Namespace: namespace}
if err := c.Get(context.Background(), key, obj); err != nil {
return nil, err
}
return obj, nil
}
// CloneTemplate uses the client and the reference to create a new object from the template.
func CloneTemplate(c client.Client, ref *corev1.ObjectReference, namespace string) (*unstructured.Unstructured, error) {
from, err := Get(c, ref, namespace)
if err != nil {
return nil, err
}
template, found, err := unstructured.NestedMap(from.Object, "spec", "template")
if !found {
return nil, errors.Errorf("missing Spec.Template on %v %q", from.GroupVersionKind(), from.GetName())
} else if err != nil {
return nil, errors.Wrapf(err, "failed to retrieve Spec.Template map on %v %q", from.GroupVersionKind(), from.GetName())
}
// Create the unstructured object from the template.
to := &unstructured.Unstructured{Object: template}
to.SetResourceVersion("")
to.SetOwnerReferences(nil)
to.SetFinalizers(nil)
to.SetUID("")
to.SetSelfLink("")
to.SetName("")
to.SetGenerateName(fmt.Sprintf("%s-", from.GetName()))
to.SetNamespace(namespace)
// Set the object APIVersion.
if to.GetAPIVersion() == "" {
to.SetAPIVersion(ref.APIVersion)
}
// Set the object Kind and strip the word "Template" if it's a suffix.
if to.GetKind() == "" {
to.SetKind(strings.TrimSuffix(ref.Kind, "Template"))
}
// Create the external clone.
if err := c.Create(context.Background(), to); err != nil {
return nil, err
}
return to, nil
}
// ErrorsFrom returns the ErrorReason and ErrorMessage fields from the external object status.
func ErrorsFrom(obj *unstructured.Unstructured) (string, string, error) {
errorReason, _, err := unstructured.NestedString(obj.Object, "status", "errorReason")
if err != nil {
return "", "", errors.Wrapf(err, "failed to determine errorReason on %v %q",
obj.GroupVersionKind(), obj.GetName())
}
errorMessage, _, err := unstructured.NestedString(obj.Object, "status", "errorMessage")
if err != nil {
return "", "", errors.Wrapf(err, "failed to determine errorMessage on %v %q",
obj.GroupVersionKind(), obj.GetName())
}
return errorReason, errorMessage, nil
}
// IsReady returns true if the Status.Ready field on an external object is true.
func IsReady(obj *unstructured.Unstructured) (bool, error) {
ready, found, err := unstructured.NestedBool(obj.Object, "status", "ready")
if err != nil {
return false, errors.Wrapf(err, "failed to determine %v %q readiness",
obj.GroupVersionKind(), obj.GetName())
}
return ready && found, nil
}