| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package runtimeutil |
| 5 |
|
| 6 |
import ( |
| 7 |
"fmt" |
| 8 |
"os" |
| 9 |
"sort" |
| 10 |
"strings" |
| 11 |
|
| 12 |
"sigs.k8s.io/kustomize/kyaml/yaml" |
| 13 |
k8syaml "sigs.k8s.io/yaml" |
| 14 |
) |
| 15 |
|
| 16 |
const ( |
| 17 |
FunctionAnnotationKey = "config.kubernetes.io/function" |
| 18 |
oldFunctionAnnotationKey = "config.k8s.io/function" |
| 19 |
) |
| 20 |
|
| 21 |
var functionAnnotationKeys = []string{FunctionAnnotationKey, oldFunctionAnnotationKey} |
| 22 |
|
| 23 |
|
| 24 |
type ContainerNetworkName string |
| 25 |
|
| 26 |
const ( |
| 27 |
NetworkNameNone ContainerNetworkName = "none" |
| 28 |
NetworkNameHost ContainerNetworkName = "host" |
| 29 |
) |
| 30 |
const defaultEnvValue string = "true" |
| 31 |
|
| 32 |
|
| 33 |
type ContainerEnv struct { |
| 34 |
|
| 35 |
EnvVars map[string]string |
| 36 |
|
| 37 |
|
| 38 |
VarsToExport []string |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
func (ce *ContainerEnv) GetDockerFlags() []string { |
| 43 |
envs := ce.EnvVars |
| 44 |
if envs == nil { |
| 45 |
envs = make(map[string]string) |
| 46 |
} |
| 47 |
|
| 48 |
flags := []string{} |
| 49 |
|
| 50 |
keys := []string{} |
| 51 |
for k := range envs { |
| 52 |
keys = append(keys, k) |
| 53 |
} |
| 54 |
sort.Strings(keys) |
| 55 |
for _, key := range keys { |
| 56 |
flags = append(flags, "-e", key+"="+envs[key]) |
| 57 |
} |
| 58 |
|
| 59 |
for _, key := range ce.VarsToExport { |
| 60 |
flags = append(flags, "-e", key) |
| 61 |
} |
| 62 |
|
| 63 |
return flags |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
func (ce *ContainerEnv) AddKeyValue(key, value string) { |
| 68 |
if ce.EnvVars == nil { |
| 69 |
ce.EnvVars = make(map[string]string) |
| 70 |
} |
| 71 |
ce.EnvVars[key] = value |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
func (ce *ContainerEnv) HasExportedKey(key string) bool { |
| 76 |
for _, k := range ce.VarsToExport { |
| 77 |
if k == key { |
| 78 |
return true |
| 79 |
} |
| 80 |
} |
| 81 |
return false |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
func (ce *ContainerEnv) AddKey(key string) { |
| 86 |
if !ce.HasExportedKey(key) { |
| 87 |
ce.VarsToExport = append(ce.VarsToExport, key) |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
func (ce *ContainerEnv) Raw() []string { |
| 94 |
var ret []string |
| 95 |
for k, v := range ce.EnvVars { |
| 96 |
ret = append(ret, k+"="+v) |
| 97 |
} |
| 98 |
|
| 99 |
ret = append(ret, ce.VarsToExport...) |
| 100 |
return ret |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
func NewContainerEnv() *ContainerEnv { |
| 105 |
var ce ContainerEnv |
| 106 |
ce.EnvVars = make(map[string]string) |
| 107 |
|
| 108 |
ce.EnvVars["LOG_TO_STDERR"] = defaultEnvValue |
| 109 |
ce.EnvVars["STRUCTURED_RESULTS"] = defaultEnvValue |
| 110 |
return &ce |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
func NewContainerEnvFromStringSlice(envStr []string) *ContainerEnv { |
| 116 |
ce := NewContainerEnv() |
| 117 |
for _, e := range envStr { |
| 118 |
parts := strings.SplitN(e, "=", 2) |
| 119 |
if len(parts) == 1 { |
| 120 |
ce.AddKey(e) |
| 121 |
} else { |
| 122 |
ce.AddKeyValue(parts[0], parts[1]) |
| 123 |
} |
| 124 |
} |
| 125 |
return ce |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
type FunctionSpec struct { |
| 130 |
DeferFailure bool `json:"deferFailure,omitempty" yaml:"deferFailure,omitempty"` |
| 131 |
|
| 132 |
|
| 133 |
Container ContainerSpec `json:"container,omitempty" yaml:"container,omitempty"` |
| 134 |
|
| 135 |
|
| 136 |
Exec ExecSpec `json:"exec,omitempty" yaml:"exec,omitempty"` |
| 137 |
} |
| 138 |
|
| 139 |
type ExecSpec struct { |
| 140 |
Path string `json:"path,omitempty" yaml:"path,omitempty"` |
| 141 |
|
| 142 |
|
| 143 |
Args []string `json:"args,omitempty" yaml:"args,omitempty"` |
| 144 |
|
| 145 |
|
| 146 |
Env []string `json:"envs,omitempty" yaml:"envs,omitempty"` |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
type ContainerSpec struct { |
| 151 |
|
| 152 |
Image string `json:"image,omitempty" yaml:"image,omitempty"` |
| 153 |
|
| 154 |
|
| 155 |
Network bool `json:"network,omitempty" yaml:"network,omitempty"` |
| 156 |
|
| 157 |
|
| 158 |
StorageMounts []StorageMount `json:"mounts,omitempty" yaml:"mounts,omitempty"` |
| 159 |
|
| 160 |
|
| 161 |
Env []string `json:"envs,omitempty" yaml:"envs,omitempty"` |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
type StorageMount struct { |
| 166 |
|
| 167 |
MountType string `json:"type,omitempty" yaml:"type,omitempty"` |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
Src string `json:"src,omitempty" yaml:"src,omitempty"` |
| 174 |
|
| 175 |
|
| 176 |
DstPath string `json:"dst,omitempty" yaml:"dst,omitempty"` |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
ReadWriteMode bool `json:"rw,omitempty" yaml:"rw,omitempty"` |
| 181 |
} |
| 182 |
|
| 183 |
func (s *StorageMount) String() string { |
| 184 |
mode := "" |
| 185 |
if !s.ReadWriteMode { |
| 186 |
mode = ",readonly" |
| 187 |
} |
| 188 |
return fmt.Sprintf("type=%s,source=%s,target=%s%s", s.MountType, s.Src, s.DstPath, mode) |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
func GetFunctionSpec(n *yaml.RNode) (*FunctionSpec, error) { |
| 197 |
meta, err := n.GetMeta() |
| 198 |
if err != nil { |
| 199 |
return nil, fmt.Errorf("failed to get ResourceMeta: %w", err) |
| 200 |
} |
| 201 |
|
| 202 |
fn, err := getFunctionSpecFromAnnotation(n, meta) |
| 203 |
if err != nil { |
| 204 |
return nil, err |
| 205 |
} |
| 206 |
if fn != nil { |
| 207 |
return fn, nil |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
container := meta.Annotations["config.kubernetes.io/container"] |
| 212 |
if container != "" { |
| 213 |
return &FunctionSpec{Container: ContainerSpec{Image: container}}, nil |
| 214 |
} |
| 215 |
return nil, nil |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
func getFunctionSpecFromAnnotation(n *yaml.RNode, meta yaml.ResourceMeta) (*FunctionSpec, error) { |
| 221 |
var fs FunctionSpec |
| 222 |
for _, s := range functionAnnotationKeys { |
| 223 |
fn := meta.Annotations[s] |
| 224 |
if fn != "" { |
| 225 |
if err := k8syaml.UnmarshalStrict([]byte(fn), &fs); err != nil { |
| 226 |
return nil, fmt.Errorf("%s unmarshal error: %w", s, err) |
| 227 |
} |
| 228 |
return &fs, nil |
| 229 |
} |
| 230 |
} |
| 231 |
n, err := n.Pipe(yaml.Lookup("metadata", "configFn")) |
| 232 |
if err != nil { |
| 233 |
return nil, fmt.Errorf("failed to look up metadata.configFn: %w", err) |
| 234 |
} |
| 235 |
if yaml.IsMissingOrNull(n) { |
| 236 |
return nil, nil |
| 237 |
} |
| 238 |
s, err := n.String() |
| 239 |
if err != nil { |
| 240 |
fmt.Fprintf(os.Stderr, "configFn parse error: %v\n", err) |
| 241 |
return nil, fmt.Errorf("configFn parse error: %w", err) |
| 242 |
} |
| 243 |
if err := k8syaml.UnmarshalStrict([]byte(s), &fs); err != nil { |
| 244 |
return nil, fmt.Errorf("%s unmarshal error: %w", "configFn", err) |
| 245 |
} |
| 246 |
return &fs, nil |
| 247 |
} |
| 248 |
|
| 249 |
func StringToStorageMount(s string) StorageMount { |
| 250 |
m := make(map[string]string) |
| 251 |
options := strings.Split(s, ",") |
| 252 |
for _, option := range options { |
| 253 |
keyVal := strings.SplitN(option, "=", 2) |
| 254 |
if len(keyVal) == 2 { |
| 255 |
m[keyVal[0]] = keyVal[1] |
| 256 |
} |
| 257 |
} |
| 258 |
var sm StorageMount |
| 259 |
for key, value := range m { |
| 260 |
switch { |
| 261 |
case key == "type": |
| 262 |
sm.MountType = value |
| 263 |
case key == "src" || key == "source": |
| 264 |
sm.Src = value |
| 265 |
case key == "dst" || key == "target": |
| 266 |
sm.DstPath = value |
| 267 |
case key == "rw" && value == "true": |
| 268 |
sm.ReadWriteMode = true |
| 269 |
} |
| 270 |
} |
| 271 |
return sm |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
type IsReconcilerFilter struct { |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
ExcludeReconcilers bool `yaml:"excludeReconcilers,omitempty"` |
| 282 |
|
| 283 |
|
| 284 |
IncludeNonReconcilers bool `yaml:"includeNonReconcilers,omitempty"` |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
func (c *IsReconcilerFilter) Filter(inputs []*yaml.RNode) ([]*yaml.RNode, error) { |
| 289 |
var out []*yaml.RNode |
| 290 |
for i := range inputs { |
| 291 |
functionSpec, err := GetFunctionSpec(inputs[i]) |
| 292 |
if err != nil { |
| 293 |
return nil, err |
| 294 |
} |
| 295 |
isFnResource := functionSpec != nil |
| 296 |
if isFnResource && !c.ExcludeReconcilers { |
| 297 |
out = append(out, inputs[i]) |
| 298 |
} |
| 299 |
if !isFnResource && c.IncludeNonReconcilers { |
| 300 |
out = append(out, inputs[i]) |
| 301 |
} |
| 302 |
} |
| 303 |
return out, nil |
| 304 |
} |