-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathload.go
102 lines (91 loc) · 2.63 KB
/
load.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
package configuration
import (
"bufio"
"context"
"io"
"os"
"github.com/google/go-containerregistry/pkg/v1/mutate"
"github.com/web-seven/overlock/internal/image"
"github.com/web-seven/overlock/internal/kube"
"github.com/web-seven/overlock/internal/packages"
"github.com/web-seven/overlock/internal/registry"
"go.uber.org/zap"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/rest"
)
func (c *Configuration) UpgradeConfiguration(ctx context.Context, config *rest.Config, dc *dynamic.DynamicClient) error {
cfgs := GetConfigurations(ctx, dc)
var pkgs []packages.Package
for _, c := range cfgs {
pkg := packages.Package{
Name: c.Name,
Url: c.Spec.Package,
}
pkgs = append(pkgs, pkg)
}
var err error
c.Name, err = c.UpgradeVersion(ctx, dc, c.Name, pkgs)
if err != nil {
return err
}
return nil
}
// Load configuration package from path
func (c *Configuration) LoadPathArchive(ctx context.Context, config *rest.Config, logger *zap.SugaredLogger, path string) error {
err := c.Image.LoadPathArchive(path)
if err != nil {
return err
}
return c.load(ctx, config, logger)
}
// Load configuration package from STDIN
func (c *Configuration) LoadStdinArchive(ctx context.Context, config *rest.Config, logger *zap.SugaredLogger, stream *bufio.Reader) error {
stdin, err := io.ReadAll(stream)
if err != nil {
return err
}
tmpFile, err := os.CreateTemp("", "overlock-configuration-*")
if err != nil {
return err
}
tmpFile.Write(stdin)
err = c.Image.LoadPathArchive(tmpFile.Name())
if err != nil {
return err
}
return c.load(ctx, config, logger)
}
// Load configuration package from directory
func (c *Configuration) LoadDirectory(ctx context.Context, config *rest.Config, logger *zap.SugaredLogger, path string) error {
packageLayer, err := image.LoadPackageLayerDirectory(ctx, config, path, []string{"Configuration", "CompositeResourceDefinition", "Composition"})
if err != nil {
return err
}
c.Image.Image, err = mutate.AppendLayers(c.Image, packageLayer)
if err != nil {
return err
}
return c.load(ctx, config, logger)
}
// Load configuration to registry
func (c *Configuration) load(ctx context.Context, config *rest.Config, logger *zap.SugaredLogger) error {
client, err := kube.Client(config)
if err != nil {
return err
}
isLocal, err := registry.IsLocalRegistry(ctx, client)
if !isLocal || err != nil {
reg := registry.NewLocal()
reg.SetDefault(true)
err := reg.Create(ctx, config, logger)
if err != nil {
return err
}
}
err = registry.PushLocalRegistry(ctx, c.Name, c.Image, config, logger)
if err != nil {
return err
}
logger.Infof("Image archive %s loaded to local registry.", c.Name)
return nil
}