This repository has been archived by the owner on Feb 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
apply.go
181 lines (142 loc) · 4.47 KB
/
apply.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package omnictl
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"os"
"github.com/cosi-project/runtime/pkg/resource"
"github.com/cosi-project/runtime/pkg/resource/protobuf"
"github.com/cosi-project/runtime/pkg/state"
"github.com/sergi/go-diff/diffmatchpatch"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
"github.com/siderolabs/omni-client/pkg/client"
"github.com/siderolabs/omni-client/pkg/omnictl/internal/access"
)
var applyCmdFlags struct {
resFile string
options options
}
// applyCmd represents apply config command.
var applyCmd = &cobra.Command{
Use: "apply",
Short: "Create or update resource using YAML file as an input",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
yamlRaw, err := os.ReadFile(applyCmdFlags.resFile)
if err != nil {
return fmt.Errorf("failed to read resource yaml file %q: %w", applyCmdFlags.resFile, err)
}
if applyCmdFlags.options.dryRun {
applyCmdFlags.options.verbose = true
}
return access.WithClient(applyConfig(yamlRaw))
},
}
func applyConfig(yamlRaw []byte) func(ctx context.Context, client *client.Client) error {
return func(ctx context.Context, client *client.Client) error {
st := client.Omni().State()
dec := yaml.NewDecoder(bytes.NewReader(yamlRaw))
var resources []resource.Resource
for {
var res protobuf.YAMLResource
err := dec.Decode(&res)
if errors.Is(err, io.EOF) {
break
}
if err != nil {
return err
}
resources = append(resources, res.Resource())
}
for _, res := range resources {
got, err := st.Get(ctx, res.Metadata())
if err != nil && !state.IsNotFoundError(err) {
return fmt.Errorf("failed to get resource '%s' '%s': %w", res.Metadata().ID(), res.Metadata().Type(), err)
}
if state.IsNotFoundError(err) {
err = createResource(ctx, st, res, applyCmdFlags.options)
if err != nil {
return err
}
continue
}
err = updateResource(ctx, st, got, res, applyCmdFlags.options)
if err != nil {
return fmt.Errorf("failed to update resource '%s' '%s': %w", res.Metadata().ID(), res.Metadata().Type(), err)
}
}
return nil
}
}
type options struct {
dryRun bool
verbose bool
}
func createResource(ctx context.Context, st state.State, res resource.Resource, opts options) error {
if opts.verbose {
out, err := marshalResource(res)
if err != nil {
return err
}
fmt.Printf("Creating resource '%s'\n\n%s\n\n", res.Metadata().ID(), out)
}
if opts.dryRun {
return nil
}
if err := st.Create(ctx, res); err != nil {
return fmt.Errorf("failed to create resource '%s' '%s': %w", res.Metadata().ID(), res.Metadata().Type(), err)
}
return nil
}
func updateResource(ctx context.Context, st state.State, got resource.Resource, res resource.Resource, opts options) error {
if opts.verbose {
outGot, err := marshalResource(got)
if err != nil {
return err
}
outRes, err := marshalResource(res)
if err != nil {
return err
}
dmp := diffmatchpatch.New()
diffs := dmp.DiffMain(outGot, outRes, false)
fmt.Printf("Updating resource '%s'\n\n%s\n\n", res.Metadata().ID(), dmp.DiffPrettyText(diffs))
}
if opts.dryRun {
return nil
}
res.Metadata().SetVersion(got.Metadata().Version())
if err := st.Update(ctx, res); err != nil {
return fmt.Errorf("failed to update resource '%s' '%s': %w", res.Metadata().ID(), res.Metadata().Type(), err)
}
return nil
}
func marshalResource(res resource.Resource) (string, error) {
yamlRes, err := resource.MarshalYAML(res)
if err != nil {
return "", fmt.Errorf("failed to marshal resource '%s' '%s': %w", res.Metadata().ID(), res.Metadata().Type(), err)
}
out, err := yaml.Marshal(yamlRes)
if err != nil {
return "", fmt.Errorf("failed to marshal resource '%s' '%s': %w", res.Metadata().ID(), res.Metadata().Type(), err)
}
return string(out), nil
}
func init() {
applyCmd.PersistentFlags().StringVarP(&applyCmdFlags.resFile, "file", "f", "", "Resource file to load and apply")
applyCmd.PersistentFlags().BoolVarP(&applyCmdFlags.options.verbose, "verbose", "v", false, "Verbose output")
applyCmd.PersistentFlags().BoolVarP(&applyCmdFlags.options.dryRun, "dry-run", "d", false, "Dry run, implies verbose")
must(applyCmd.MarkPersistentFlagRequired("file"))
RootCmd.AddCommand(applyCmd)
}
func must(err error) {
if err != nil {
panic(err)
}
}