|
| 1 | +// Copyright 2020-Present VMware, Inc. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package clusterbuildpack |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + |
| 9 | + "github.com/google/go-containerregistry/pkg/authn" |
| 10 | + "github.com/pivotal/kpack/pkg/apis/build/v1alpha2" |
| 11 | + corev1alpha1 "github.com/pivotal/kpack/pkg/apis/core/v1alpha1" |
| 12 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 13 | + |
| 14 | + "github.com/buildpacks-community/kpack-cli/pkg/buildpackage" |
| 15 | + "github.com/buildpacks-community/kpack-cli/pkg/config" |
| 16 | + "github.com/buildpacks-community/kpack-cli/pkg/k8s" |
| 17 | + "github.com/buildpacks-community/kpack-cli/pkg/registry" |
| 18 | +) |
| 19 | + |
| 20 | +type BuildpackageUploader interface { |
| 21 | + UploadBuildpackage(keychain authn.Keychain, buildPackage, repository string) (string, error) |
| 22 | + ValidateBuildpackImage(keychain authn.Keychain, imageTag string) error |
| 23 | +} |
| 24 | + |
| 25 | +type Printer interface { |
| 26 | + Printlnf(format string, args ...interface{}) error |
| 27 | + PrintStatus(format string, args ...interface{}) error |
| 28 | +} |
| 29 | + |
| 30 | +type Factory struct { |
| 31 | + Uploader BuildpackageUploader |
| 32 | + Printer Printer |
| 33 | +} |
| 34 | + |
| 35 | +func NewFactory(printer Printer, relocator registry.Relocator, fetcher registry.Fetcher) *Factory { |
| 36 | + return &Factory{ |
| 37 | + Uploader: &buildpackage.Uploader{ |
| 38 | + Fetcher: fetcher, |
| 39 | + Relocator: relocator, |
| 40 | + }, |
| 41 | + Printer: printer, |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func (f *Factory) MakeBuildpack(keychain authn.Keychain, name, imageTag string, kpConfig config.KpConfig) (*v1alpha2.ClusterBuildpack, error) { |
| 46 | + err := f.validate(keychain, imageTag) |
| 47 | + if err != nil { |
| 48 | + return nil, fmt.Errorf("invalid buildpack image: %w", err) |
| 49 | + } |
| 50 | + |
| 51 | + defaultRepo, err := kpConfig.DefaultRepository() |
| 52 | + if err != nil { |
| 53 | + return nil, err |
| 54 | + } |
| 55 | + |
| 56 | + if err := f.Printer.PrintStatus("Uploading to '%s'...", defaultRepo); err != nil { |
| 57 | + return nil, err |
| 58 | + } |
| 59 | + |
| 60 | + relocatedImageRef, err := f.Uploader.UploadBuildpackage(keychain, imageTag, defaultRepo) |
| 61 | + if err != nil { |
| 62 | + return nil, err |
| 63 | + } |
| 64 | + |
| 65 | + sa := kpConfig.ServiceAccount() |
| 66 | + |
| 67 | + buildpack := &v1alpha2.ClusterBuildpack{ |
| 68 | + TypeMeta: metav1.TypeMeta{ |
| 69 | + Kind: v1alpha2.ClusterBuildpackKind, |
| 70 | + APIVersion: "kpack.io/v1alpha2", |
| 71 | + }, |
| 72 | + ObjectMeta: metav1.ObjectMeta{ |
| 73 | + Name: name, |
| 74 | + Annotations: map[string]string{}, |
| 75 | + }, |
| 76 | + Spec: v1alpha2.ClusterBuildpackSpec{ |
| 77 | + ImageSource: corev1alpha1.ImageSource{ |
| 78 | + Image: relocatedImageRef, |
| 79 | + }, |
| 80 | + ServiceAccountRef: &sa, |
| 81 | + }, |
| 82 | + } |
| 83 | + |
| 84 | + return buildpack, k8s.SetLastAppliedCfg(buildpack) |
| 85 | +} |
| 86 | + |
| 87 | +func (f *Factory) UpdateBuildpack(keychain authn.Keychain, buildpack *v1alpha2.ClusterBuildpack, imageTag string, kpConfig config.KpConfig) (*v1alpha2.ClusterBuildpack, error) { |
| 88 | + err := f.validate(keychain, imageTag) |
| 89 | + if err != nil { |
| 90 | + return nil, fmt.Errorf("invalid buildpack image: %w", err) |
| 91 | + } |
| 92 | + |
| 93 | + defaultRepo, err := kpConfig.DefaultRepository() |
| 94 | + if err != nil { |
| 95 | + return nil, err |
| 96 | + } |
| 97 | + |
| 98 | + if err := f.Printer.PrintStatus("Uploading to '%s'...", defaultRepo); err != nil { |
| 99 | + return nil, err |
| 100 | + } |
| 101 | + |
| 102 | + relocatedImageRef, err := f.Uploader.UploadBuildpackage(keychain, imageTag, defaultRepo) |
| 103 | + if err != nil { |
| 104 | + return nil, err |
| 105 | + } |
| 106 | + |
| 107 | + newBuildpack := buildpack.DeepCopy() |
| 108 | + newBuildpack.Spec.ImageSource.Image = relocatedImageRef |
| 109 | + return newBuildpack, nil |
| 110 | +} |
| 111 | + |
| 112 | +func (f *Factory) validate(keychain authn.Keychain, imageTag string) error { |
| 113 | + return f.Uploader.ValidateBuildpackImage(keychain, imageTag) |
| 114 | +} |
0 commit comments