-
Notifications
You must be signed in to change notification settings - Fork 103
/
package.go
129 lines (109 loc) · 3.25 KB
/
package.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
package install
import (
"strings"
"time"
"github.com/kudobuilder/kudo/pkg/apis/kudo/v1beta1"
"github.com/kudobuilder/kudo/pkg/kudoctl/clog"
"github.com/kudobuilder/kudo/pkg/kudoctl/packages"
"github.com/kudobuilder/kudo/pkg/kudoctl/packages/resolver"
"github.com/kudobuilder/kudo/pkg/kudoctl/resources/install"
"github.com/kudobuilder/kudo/pkg/kudoctl/util/kudo"
)
type Options struct {
// Skip instance resource creation.
SkipInstance bool
// Wait until the instance has been created.
Wait *time.Duration
// Create the namespace for the operator package.
CreateNamespace bool
}
// Package installs an operator package with parameters into a namespace.
// Instance name, namespace and operator parameters are applied to the
// operator package resources. These rendered resources are then created
// on the Kubernetes cluster.
// Packages can have dependencies on other packages. In that case,
// dependent packages are resolved and their Operator and
// Operatorversion resources created on the Kubernetes cluster.
func Package(
client *kudo.Client,
instanceName string,
namespace string,
resources packages.Resources,
parameters map[string]string,
resolver resolver.Resolver,
options Options) error {
clog.V(3).Printf(
"Preparing %s/%s:%s for installation",
namespace,
resources.Operator.Name,
resources.OperatorVersion.Spec.Version)
applyOverrides(&resources, instanceName, namespace, parameters)
if err := validateParameters(
*resources.Instance,
resources.OperatorVersion.Spec.Parameters); err != nil {
return err
}
if err := client.ValidateServerForOperator(resources.Operator); err != nil {
return err
}
if options.CreateNamespace {
if err := installNamespace(client, resources, parameters); err != nil {
return err
}
}
if err := install.OperatorAndOperatorVersion(
client, resources.Operator, resources.OperatorVersion, resolver); err != nil {
return err
}
if options.SkipInstance {
return nil
}
if err := install.Instance(client, resources.Instance); err != nil {
return err
}
if options.Wait != nil {
if err := install.WaitForInstance(client, resources.Instance, *options.Wait); err != nil {
return err
}
}
return nil
}
func applyOverrides(
resources *packages.Resources,
instanceName string,
namespace string,
parameters map[string]string) {
resources.Operator.SetNamespace(namespace)
resources.OperatorVersion.SetNamespace(namespace)
resources.Instance.SetNamespace(namespace)
if instanceName != "" {
clog.V(3).Printf(
"Overriding instance name %s/%s to %s/%s",
namespace,
resources.Instance.Name,
namespace,
instanceName)
resources.Instance.SetName(instanceName)
}
if parameters != nil {
clog.V(3).Printf("parameters in use: %v", parameters)
resources.Instance.Spec.Parameters = parameters
}
}
func validateParameters(instance v1beta1.Instance, parameters []v1beta1.Parameter) error {
missingParameters := []string{}
for _, p := range parameters {
if *p.Required && p.Default == nil {
_, ok := instance.Spec.Parameters[p.Name]
if !ok {
missingParameters = append(missingParameters, p.Name)
}
}
}
if len(missingParameters) > 0 {
return clog.Errorf(
"missing required parameters during installation: %s",
strings.Join(missingParameters, ","))
}
return nil
}