-
Notifications
You must be signed in to change notification settings - Fork 19
/
agentconfig_types.go
140 lines (115 loc) · 4.87 KB
/
agentconfig_types.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
package v1
import (
"fmt"
"github.com/mitchellh/mapstructure"
"github.com/opencontainers/go-digest"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// AgentConfigSpec defines the configuration for the Porter agent.
//
// SERIALIZATION NOTE:
// The json serialization is for persisting this to Kubernetes.
// The mapstructure tags is used internally for AgentConfigSpec.MergeConfig.
type AgentConfigSpec struct {
// PorterRepository is the repository for the Porter Agent image.
// Defaults to ghcr.io/getporter/porter-agent
PorterRepository string `json:"porterRepository,omitempty" mapstructure:"porterRepository,omitempty"`
// PorterVersion is the tag for the Porter Agent image.
// Defaults to a well-known version of the agent that has been tested with the operator.
// Users SHOULD override this to use more recent versions.
PorterVersion string `json:"porterVersion,omitempty" mapstructure:"porterVersion,omitempty"`
// ServiceAccount is the service account to run the Porter Agent under.
ServiceAccount string `json:"serviceAccount,omitempty" mapstructure:"serviceAccount,omitempty"`
// VolumeSize is the size of the persistent volume that Porter will
// request when running the Porter Agent. It is used to share data
// between the Porter Agent and the bundle invocation image. It must
// be large enough to store any files used by the bundle including credentials,
// parameters and outputs.
VolumeSize string `json:"volumeSize,omitempty" mapstructure:"volumeSize,omitempty"`
// PullPolicy specifies when to pull the Porter Agent image. The default
// is to use PullAlways when the tag is canary or latest, and PullIfNotPresent
// otherwise.
PullPolicy v1.PullPolicy `json:"pullPolicy,omitempty" mapstructure:"pullPolicy,omitempty"`
// InstallationServiceAccount specifies a service account to run the Kubernetes pod/job for the installation image.
// The default is to run without a service account.
// This can be useful for a bundle which is targeting the kubernetes cluster that the operator is installed in.
InstallationServiceAccount string `json:"installationServiceAccount,omitempty" mapstructure:"installationServiceAccount,omitempty"`
}
// GetPorterImage returns the fully qualified image name of the Porter Agent
// image. Defaults the repository and tag when not set.
func (c AgentConfigSpec) GetPorterImage() string {
version := c.PorterVersion
if version == "" {
// We don't use a mutable tag like latest, or canary because it's a bad practice that we don't want to encourage.
version = DefaultPorterAgentVersion
}
repo := c.PorterRepository
if repo == "" {
repo = DefaultPorterAgentRepository
}
if digest, err := digest.Parse(version); err == nil {
return fmt.Sprintf("%s@%s", repo, digest)
}
return fmt.Sprintf("%s:%s", repo, version)
}
// GetPullPolicy returns the PullPolicy that should be used for the Porter Agent
// (not the bundle). Defaults to PullAlways for latest and canary,
// PullIfNotPresent otherwise.
func (c AgentConfigSpec) GetPullPolicy() v1.PullPolicy {
if c.PullPolicy != "" {
return c.PullPolicy
}
if c.PorterVersion == "latest" || c.PorterVersion == "canary" || c.PorterVersion == "dev" {
return v1.PullAlways
}
return v1.PullIfNotPresent
}
// GetVolumeSize returns the size of the shared volume to mount between the
// Porter Agent and the bundle's invocation image. Defaults to 64Mi.
func (c AgentConfigSpec) GetVolumeSize() resource.Quantity {
q, err := resource.ParseQuantity(c.VolumeSize)
if err != nil || q.IsZero() {
return resource.MustParse("64Mi")
}
return q
}
// MergeConfig from another AgentConfigSpec. The values from the override are applied
// only when they are not empty.
func (c AgentConfigSpec) MergeConfig(overrides ...AgentConfigSpec) (AgentConfigSpec, error) {
final := c
var targetRaw map[string]interface{}
if err := mapstructure.Decode(c, &targetRaw); err != nil {
return AgentConfigSpec{}, err
}
for _, override := range overrides {
var overrideRaw map[string]interface{}
if err := mapstructure.Decode(override, &overrideRaw); err != nil {
return AgentConfigSpec{}, err
}
targetRaw = MergeMap(targetRaw, overrideRaw)
}
if err := mapstructure.Decode(targetRaw, &final); err != nil {
return AgentConfigSpec{}, err
}
return final, nil
}
// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// AgentConfig is the Schema for the agentconfigs API
type AgentConfig struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec AgentConfigSpec `json:"spec,omitempty"`
}
// +kubebuilder:object:root=true
// AgentConfigList contains a list of AgentConfig values.
type AgentConfigList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []AgentConfig `json:"items"`
}
func init() {
SchemeBuilder.Register(&AgentConfig{}, &AgentConfigList{})
}