-
Notifications
You must be signed in to change notification settings - Fork 11
/
helper.go
65 lines (54 loc) · 2.41 KB
/
helper.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
package helper
import (
"fmt"
extensionscontroller "github.com/gardener/gardener/extensions/pkg/controller"
extensionsv1alpha1 "github.com/gardener/gardener/pkg/apis/extensions/v1alpha1"
"github.com/metal-stack/gardener-extension-provider-metal/pkg/apis/metal"
corev1 "k8s.io/api/core/v1"
)
// FindMachineImage takes a list of machine images and tries to find the first entry
// whose name, version, and zone matches with the given name, version, and zone. If no such entry is
// found then an error will be returned.
func FindMachineImage(machineImages []metal.MachineImage, name, version string) (*metal.MachineImage, error) {
for _, machineImage := range machineImages {
if machineImage.Name == name && machineImage.Version == version {
return &machineImage, nil
}
}
return nil, fmt.Errorf("no machine image with name %q, version %q found", name, version)
}
// FindMetalControlPlane returns the metal control plane from a given cluster spec
func FindMetalControlPlane(cloudProfileConfig *metal.CloudProfileConfig, partition string) (*metal.MetalControlPlane, *metal.Partition, error) {
for _, mcp := range cloudProfileConfig.MetalControlPlanes {
for partitionName, p := range mcp.Partitions {
if partitionName == partition {
return &mcp, &p, nil
}
}
}
return nil, nil, fmt.Errorf("no metal control plane found for partition %s in cloud profile config", partition)
}
// ImagePullPolicyFromString returns an image pull policy from string
// If the pull policy is unknown it returns "IfNotPresent"
func ImagePullPolicyFromString(policy string) corev1.PullPolicy {
switch p := corev1.PullPolicy(policy); p {
case corev1.PullAlways, corev1.PullIfNotPresent, corev1.PullNever:
return p
default:
return corev1.PullIfNotPresent
}
}
// GetNodeCIDR returns the node cidr from the shoot spec. if this is not yet set, it returns the
// node cidr from the infrastructure status. if it's set nowhere, it returns an error.
func GetNodeCIDR(infrastructure *extensionsv1alpha1.Infrastructure, cluster *extensionscontroller.Cluster) (string, error) {
var nodeCIDR string
if cluster.Shoot.Spec.Networking != nil && cluster.Shoot.Spec.Networking.Nodes != nil {
nodeCIDR = *cluster.Shoot.Spec.Networking.Nodes
} else if infrastructure != nil && infrastructure.Status.NodesCIDR != nil {
nodeCIDR = *infrastructure.Status.NodesCIDR
}
if nodeCIDR == "" {
return "", fmt.Errorf("nodeCIDR is not yet set")
}
return nodeCIDR, nil
}