-
Notifications
You must be signed in to change notification settings - Fork 911
/
installer.go
132 lines (119 loc) · 4.29 KB
/
installer.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
/*
Copyright 2021 The Crossplane Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package initializer
import (
"context"
"github.com/google/go-containerregistry/pkg/name"
"github.com/pkg/errors"
kerrors "k8s.io/apimachinery/pkg/api/errors"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/crossplane/crossplane-runtime/pkg/resource"
v1 "github.com/crossplane/crossplane/apis/pkg/v1"
"github.com/crossplane/crossplane/internal/xpkg"
)
const (
errListProviders = "failed getting provider list"
errListConfigurations = "failed getting configuration list"
errParsePackageName = "package name is not valid"
errApplyPackage = "cannot apply package"
)
// NewPackageInstaller returns a new package installer.
func NewPackageInstaller(p []string, c []string) *PackageInstaller {
return &PackageInstaller{
providers: p,
configurations: c,
}
}
// PackageInstaller has the initializer for installing a list of packages.
type PackageInstaller struct {
configurations []string
providers []string
}
// Run makes sure all specified packages exist.
// NOTE(hasheddan): this function is over our cyclomatic complexity goal, but
// only runs at installation time and is performing fairly straightforward
// operations.
func (pi *PackageInstaller) Run(ctx context.Context, kube client.Client) error { //nolint:gocyclo
pkgs := make([]client.Object, len(pi.providers)+len(pi.configurations))
// NOTE(hasheddan): we build maps of existing Provider and Configuration
// sources to the package names such that we can update the version when a
// package specified for install matches the source of an existing package.
pl := &v1.ProviderList{}
if err := kube.List(ctx, pl); err != nil && !kerrors.IsNotFound(err) {
return errors.Wrap(err, errListProviders)
}
pMap := make(map[string]string, len(pl.Items))
for _, p := range pl.Items {
ref, err := name.ParseReference(p.GetSource(), name.WithDefaultRegistry(""))
if err != nil {
// NOTE(hasheddan): we skip package sources that are not have valid
// references because we cannot make assumptions about their
// versioning. The only case in which a package source can be an
// invalid reference is if it was preloaded into the package cache
// and its packagePullPolicy is set to Never.
continue
}
pMap[xpkg.ParsePackageSourceFromReference(ref)] = p.GetName()
}
cl := &v1.ConfigurationList{}
if err := kube.List(ctx, cl); err != nil && !kerrors.IsNotFound(err) {
return errors.Wrap(err, errListConfigurations)
}
cMap := make(map[string]string, len(cl.Items))
for _, c := range cl.Items {
ref, err := name.ParseReference(c.GetSource(), name.WithDefaultRegistry(""))
if err != nil {
continue
}
cMap[xpkg.ParsePackageSourceFromReference(ref)] = c.GetName()
}
// NOTE(hasheddan): we maintain a separate index from the range so that
// Providers and Configurations can be added to the same slice for applying.
pkgsIdx := 0
for _, img := range pi.providers {
p := &v1.Provider{}
if err := buildPack(p, img, pMap); err != nil {
return err
}
pkgs[pkgsIdx] = p
pkgsIdx++
}
for _, img := range pi.configurations {
c := &v1.Configuration{}
if err := buildPack(c, img, cMap); err != nil {
return err
}
pkgs[pkgsIdx] = c
pkgsIdx++
}
pa := resource.NewAPIPatchingApplicator(kube)
for _, p := range pkgs {
if err := pa.Apply(ctx, p); err != nil {
return errors.Wrap(err, errApplyPackage)
}
}
return nil
}
func buildPack(pack v1.Package, img string, pkgMap map[string]string) error {
ref, err := name.ParseReference(img, name.WithDefaultRegistry(""))
if err != nil {
return errors.Wrap(err, errParsePackageName)
}
objName := xpkg.ToDNSLabel(ref.Context().RepositoryStr())
if existing, ok := pkgMap[ref.Context().RepositoryStr()]; ok {
objName = existing
}
pack.SetName(objName)
pack.SetSource(ref.String())
return nil
}