-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
192 lines (164 loc) · 5.48 KB
/
main.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
182
183
184
185
186
187
188
189
190
191
192
// SPDX-License-Identifier: AGPL-3.0-or-later
/*
* Copyright (C) 2024 Damian Peckett <[email protected]>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"context"
"fmt"
"log/slog"
"os"
goruntime "runtime"
"time"
"github.com/dpeckett/airgapify/api/v1alpha1"
airgapifyv1alpha1 "github.com/dpeckett/airgapify/api/v1alpha1"
"github.com/dpeckett/airgapify/internal/archive"
"github.com/dpeckett/airgapify/internal/constants"
"github.com/dpeckett/airgapify/internal/extractor"
"github.com/dpeckett/airgapify/internal/loader"
"github.com/dpeckett/airgapify/internal/util"
"github.com/dpeckett/telemetry"
telemetryv1alpha1 "github.com/dpeckett/telemetry/v1alpha1"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/urfave/cli/v2"
"k8s.io/apimachinery/pkg/runtime"
)
func main() {
persistentFlags := []cli.Flag{
&cli.GenericFlag{
Name: "log-level",
Usage: "Set the log verbosity level",
Value: util.FromSlogLevel(slog.LevelInfo),
},
}
initLogger := func(c *cli.Context) error {
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
Level: (*slog.Level)(c.Generic("log-level").(*util.LevelFlag)),
})))
return nil
}
// Collect anonymized usage statistics.
var telemetryReporter *telemetry.Reporter
initTelemetry := func(c *cli.Context) error {
telemetryReporter = telemetry.NewReporter(c.Context, slog.Default(), telemetry.Configuration{
BaseURL: constants.TelemetryURL,
Tags: []string{"airgapify"},
})
// Some basic system information.
info := map[string]string{
"os": goruntime.GOOS,
"arch": goruntime.GOARCH,
"num_cpu": fmt.Sprintf("%d", goruntime.NumCPU()),
"version": constants.Version,
}
telemetryReporter.ReportEvent(&telemetryv1alpha1.TelemetryEvent{
Kind: telemetryv1alpha1.TelemetryEventKindInfo,
Name: "ApplicationStart",
Values: info,
})
return nil
}
shutdownTelemetry := func(c *cli.Context) error {
if telemetryReporter == nil {
return nil
}
telemetryReporter.ReportEvent(&telemetryv1alpha1.TelemetryEvent{
Kind: telemetryv1alpha1.TelemetryEventKindInfo,
Name: "ApplicationStop",
})
// Don't want to block the shutdown of the application for too long.
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
if err := telemetryReporter.Shutdown(ctx); err != nil {
slog.Error("Failed to close telemetry reporter", slog.Any("error", err))
}
return nil
}
app := &cli.App{
Name: "airgapify",
Usage: "A little tool that will construct an OCI image archive from a set of Kubernetes manifests.",
Version: constants.Version,
Flags: append([]cli.Flag{
&cli.StringSliceFlag{
Name: "file",
Aliases: []string{"f"},
Usage: "Path to one or more Kubernetes manifests.",
Required: true,
},
&cli.StringFlag{
Name: "output",
Aliases: []string{"o"},
Usage: "Where to write the OCI image archive (optionally compressed).",
Value: "images.tar",
},
&cli.StringFlag{
Name: "platform",
Aliases: []string{"p"},
Usage: "The target platform for the image archive.",
},
}, persistentFlags...),
Before: util.BeforeAll(initLogger, initTelemetry),
After: shutdownTelemetry,
Action: func(c *cli.Context) error {
objects, err := loader.LoadObjectsFromFiles(c.StringSlice("file"))
if err != nil {
return fmt.Errorf("failed to load objects: %w", err)
}
slog.Info("Loaded objects", "count", len(objects))
rules := extractor.DefaultRules
for _, obj := range objects {
if obj.GetAPIVersion() == v1alpha1.GroupVersion.String() && obj.GetKind() == "Config" {
slog.Info("Found airgapify config")
var config airgapifyv1alpha1.Config
err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &config)
if err != nil {
return fmt.Errorf("failed to convert config: %w", err)
}
for _, rule := range config.Spec.Rules {
rules = append(rules, extractor.ImageReferenceExtractionRule{
TypeMeta: rule.TypeMeta,
Paths: rule.Paths,
})
}
}
}
e := extractor.NewImageReferenceExtractor(rules)
images, err := e.ExtractImageReferences(objects)
if err != nil {
return fmt.Errorf("failed to extract image references: %w", err)
}
if images.Len() > 0 {
slog.Info("Found image references", "count", images.Len())
}
var platform *v1.Platform
if c.IsSet("platform") {
platform, err = v1.ParsePlatform(c.String("platform"))
if err != nil {
return fmt.Errorf("failed to parse platform: %w", err)
}
}
outputPath := c.String("output")
if err := archive.Create(c.Context, outputPath, images, platform); err != nil {
return fmt.Errorf("failed to create image archive: %w", err)
}
return nil
},
}
if err := app.Run(os.Args); err != nil {
slog.Error("Error", slog.Any("error", err))
os.Exit(1)
}
}