-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathfactory.go
124 lines (101 loc) · 3.61 KB
/
factory.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
// Copyright 2020-Present VMware, Inc.
// SPDX-License-Identifier: Apache-2.0
package clusterstack
import (
"github.com/google/go-containerregistry/pkg/authn"
"github.com/pivotal/kpack/pkg/apis/build/v1alpha2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/buildpacks-community/kpack-cli/pkg/config"
"github.com/buildpacks-community/kpack-cli/pkg/registry"
"github.com/buildpacks-community/kpack-cli/pkg/stackimage"
)
type Uploader interface {
UploadStackImages(keychain authn.Keychain, buildImageTag, runImageTag, dest string) (string, string, error)
ValidateStackIDs(keychain authn.Keychain, buildImageTag, runImageTag string) (string, error)
}
type Printer interface {
Printlnf(format string, args ...interface{}) error
PrintStatus(format string, args ...interface{}) error
}
type Factory struct {
Uploader Uploader
Printer Printer
}
func NewFactory(printer Printer, relocator registry.Relocator, fetcher registry.Fetcher) *Factory {
return &Factory{
Uploader: &stackimage.Uploader{
Fetcher: fetcher,
Relocator: relocator,
},
Printer: printer,
}
}
func (f *Factory) MakeStack(keychain authn.Keychain, name, buildImageTag, runImageTag string, kpConfig config.KpConfig) (*v1alpha2.ClusterStack, error) {
stackID, err := f.validate(keychain, buildImageTag, runImageTag)
if err != nil {
return nil, err
}
defaultRepo, err := kpConfig.DefaultRepository()
if err != nil {
return nil, err
}
if err := f.Printer.PrintStatus("Uploading to '%s'...", defaultRepo); err != nil {
return nil, err
}
relocatedBuildImageRef, relocatedRunImageRef, err := f.Uploader.UploadStackImages(keychain, buildImageTag, runImageTag, defaultRepo)
if err != nil {
return nil, err
}
sa := kpConfig.ServiceAccount()
return &v1alpha2.ClusterStack{
TypeMeta: metav1.TypeMeta{
Kind: v1alpha2.ClusterStackKind,
APIVersion: "kpack.io/v1alpha2",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Annotations: map[string]string{},
},
Spec: v1alpha2.ClusterStackSpec{
Id: stackID,
BuildImage: v1alpha2.ClusterStackSpecImage{
Image: relocatedBuildImageRef,
},
RunImage: v1alpha2.ClusterStackSpecImage{
Image: relocatedRunImageRef,
},
ServiceAccountRef: &sa,
},
}, nil
}
func (f *Factory) UpdateStack(keychain authn.Keychain, stack *v1alpha2.ClusterStack, buildImageTag, runImageTag string, kpConfig config.KpConfig) (*v1alpha2.ClusterStack, error) {
stackID, err := f.validate(keychain, buildImageTag, runImageTag)
if err != nil {
return nil, err
}
relocatedBuildImageRef, relocatedRunImageRef, err := f.uploadStackImages(keychain, buildImageTag, runImageTag, kpConfig)
if err != nil {
return nil, err
}
return updatedStack(stack, relocatedBuildImageRef, relocatedRunImageRef, stackID), nil
}
func (f *Factory) uploadStackImages(keychain authn.Keychain, buildImageTag, runImageTag string, kpConfig config.KpConfig) (string, string, error) {
defaultRepo, err := kpConfig.DefaultRepository()
if err != nil {
return "", "", err
}
if err := f.Printer.PrintStatus("Uploading to '%s'...", defaultRepo); err != nil {
return "", "", err
}
return f.Uploader.UploadStackImages(keychain, buildImageTag, runImageTag, defaultRepo)
}
func (f *Factory) validate(keychain authn.Keychain, buildTag, runTag string) (string, error) {
return f.Uploader.ValidateStackIDs(keychain, buildTag, runTag)
}
func updatedStack(stack *v1alpha2.ClusterStack, buildImageRef, runImageRef, stackId string) *v1alpha2.ClusterStack {
newStack := stack.DeepCopy()
newStack.Spec.BuildImage.Image = buildImageRef
newStack.Spec.RunImage.Image = runImageRef
newStack.Spec.Id = stackId
return newStack
}