1 package internal
2
3 import (
4 "context"
5 "fmt"
6 "strings"
7
8 "edge-infra.dev/pkg/f8n/warehouse/capability"
9 "edge-infra.dev/pkg/f8n/warehouse/cluster"
10 "edge-infra.dev/pkg/f8n/warehouse/lift"
11 "edge-infra.dev/pkg/f8n/warehouse/lift/unpack"
12 "edge-infra.dev/pkg/f8n/warehouse/oci/layer"
13 "edge-infra.dev/pkg/lib/cli/rags"
14 "edge-infra.dev/pkg/lib/cli/sink"
15 )
16
17 type Unpacker struct {
18 Provider cluster.Provider
19 infraNamespace string
20 infra bool
21 runtime bool
22 capabilities capability.Capabilities
23 RenderingParams map[string]string
24
25 renderingFlags map[string]*string
26 registeredParams []lift.Parameter
27 }
28
29 func NewUnpacker(p []lift.Parameter) *Unpacker {
30 return &Unpacker{registeredParams: p}
31 }
32
33 func (u *Unpacker) Options() []unpack.Option {
34 o := []unpack.Option{
35 unpack.WithInfraNamespace(u.infraNamespace),
36 unpack.ForLayerKeys(u.LayerKeys()...),
37 u.RenderOption(),
38 }
39 if u.Provider != "" {
40 o = append(o, unpack.ForProvider(u.Provider))
41 }
42 return o
43 }
44
45
46 func (u *Unpacker) LayerKeys() []string {
47 keys := []string{}
48 if u.runtime {
49 keys = append(keys, layer.Runtime.String())
50 }
51 if u.infra {
52 keys = append(keys, layer.Infra.String())
53 }
54 for _, c := range u.capabilities {
55 keys = append(keys, string(c))
56 }
57 return keys
58 }
59
60 func (u *Unpacker) RenderOption() unpack.Option {
61 return unpack.RenderWith(
62 u.RenderingParams,
63 )
64 }
65
66 func (u *Unpacker) Info(r sink.Run) {
67 values := []any{
68 "k8s-provider", u.Provider,
69 "layers", u.LayerKeys(),
70 }
71 if u.infraNamespace != "" {
72 values = append(values, "infra namespace", u.infraNamespace)
73 }
74
75 if len(u.RenderingParams) > 0 {
76 values = append(values, "parameters", u.RenderingParams)
77 }
78
79 r.Log.Info("unpacking", values...)
80 }
81
82 func (u *Unpacker) RegisterFlags(fs *rags.RagSet) {
83 u.renderingFlags = make(map[string]*string)
84 for _, r := range u.registeredParams {
85
86
87 if r.Key == lift.ClusterProviderRenderingParameter ||
88 r.Key == lift.ClusterHashRenderingParameter ||
89 r.Key == lift.PalletNameRenderingParameter {
90 continue
91 }
92
93 u.renderingFlags[r.Key] = fs.String(
94 strings.ReplaceAll(r.Key, "_", "-"),
95 "",
96 r.Description,
97 rags.WithCategory("rendering"),
98 )
99 }
100
101 fs.Var(&u.Provider, "cluster-provider",
102 "K8s cluster provider variant to unpack. This is required if the pallet "+
103 "contains multiple variants.")
104 fs.Var(&u.capabilities,
105 "runtime-capabilities",
106 "comma separated list of runtime capabilities to unpack.")
107 fs.BoolVar(&u.infra,
108 "infra", false,
109 "unpack infrastructure resources.")
110 fs.StringVar(&u.infraNamespace,
111 "infra-namespace", "",
112 "target namespace for all applied infrastructure resources, including "+
113 "dependencies. the namespace is created if it does not already exist.")
114 fs.BoolVar(&u.runtime,
115 "runtime", true,
116 "unpack the base runtime layer. --run-capabilities is needed to "+
117 "unpack additional runtime layers.")
118 }
119
120 func (u *Unpacker) BeforeRun(ctx context.Context, r sink.Run) (context.Context, sink.Run, error) {
121 u.RenderingParams = make(map[string]string)
122 for key, val := range u.renderingFlags {
123 u.RenderingParams[key] = *val
124 }
125
126 if !u.runtime && u.infraNamespace == "" {
127 return ctx, r, fmt.Errorf("if runtime resources aren't being applied, " +
128 "--infra-namespace must be provided")
129 }
130 return ctx, r, nil
131 }
132
View as plain text