forked from joeholley/supergloo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gloo_installer.go
164 lines (134 loc) · 5.03 KB
/
gloo_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
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
package gloo
import (
"context"
"fmt"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"github.com/solo-io/go-utils/installutils/helmchart"
"github.com/solo-io/go-utils/installutils/kubeinstall"
"github.com/solo-io/supergloo/pkg/util"
"github.com/solo-io/solo-kit/pkg/api/v1/resources/core"
"github.com/solo-io/go-utils/contextutils"
"github.com/solo-io/go-utils/errors"
v1 "github.com/solo-io/supergloo/pkg/api/v1"
)
const (
customResourceDefinition = "CustomResourceDefinition"
upstreamCrdName = "upstreams.gloo.solo.io"
settingsCrdName = "settings.gloo.solo.io"
)
type Installer interface {
EnsureGlooInstall(ctx context.Context, install *v1.Install, meshes v1.MeshList, meshIngresses v1.MeshIngressList) (*v1.MeshIngress, error)
}
type glooInstaller struct {
kubeInstaller kubeinstall.Installer
}
func newGlooInstaller(kubeInstaller kubeinstall.Installer) *glooInstaller {
return &glooInstaller{kubeInstaller: kubeInstaller}
}
func (i *glooInstaller) EnsureGlooInstall(ctx context.Context, install *v1.Install, meshes v1.MeshList, meshIngresses v1.MeshIngressList) (*v1.MeshIngress, error) {
ctx = contextutils.WithLogger(ctx, "gloo-ingress-installer")
logger := contextutils.LoggerFrom(ctx)
installIngress := install.GetIngress()
if installIngress == nil {
return nil, errors.Errorf("non ingress install detected in ingress install, %v", install.Metadata.Ref())
}
glooInstall := installIngress.GetGloo()
if glooInstall == nil {
return nil, errors.Errorf("%v: invalid install type, only gloo ingress supported currently", install.Metadata.Ref())
}
logger.Infof("syncing gloo install %v with config %v", install.Metadata.Ref(), glooInstall)
if install.Disabled {
logger.Infof("purging resources for disabled install %v", install.Metadata.Ref())
if err := i.kubeInstaller.PurgeResources(ctx, util.LabelsForResource(install)); err != nil {
return nil, errors.Wrapf(err, "uninstalling gloo")
}
installIngress.InstalledIngress = nil
return nil, nil
}
manifests, err := makeManifestsForInstall(ctx, install, glooInstall)
if err != nil {
return nil, err
}
rawResources, err := manifests.ResourceList()
if err != nil {
return nil, err
}
// filter out upstreams, supergloo installs them
rawResources = rawResources.Filter(func(resource *unstructured.Unstructured) bool {
if resource.GroupVersionKind().Kind == customResourceDefinition {
return resource.GetName() == upstreamCrdName || resource.GetName() == settingsCrdName
}
return false
})
installNamespace := install.InstallationNamespace
var meshIngress *v1.MeshIngress
if installIngress.InstalledIngress != nil {
var err error
meshIngress, err = meshIngresses.Find(installIngress.InstalledIngress.Strings())
if err != nil {
return nil, errors.Wrapf(err, "installed ingress not found")
}
}
var meshRefs []*core.ResourceRef
for _, glooMesh := range glooInstall.Meshes {
mesh, err := meshes.Find(glooMesh.Namespace, glooMesh.Name)
if err != nil {
return nil, errors.Wrapf(err, "target mesh not found")
}
ref := mesh.Metadata.Ref()
meshRefs = append(meshRefs, &ref)
}
logger.Infof("installing gloo with options: %#v", glooInstall)
if err := i.kubeInstaller.ReconcileResources(ctx, installNamespace, rawResources, util.LabelsForResource(install)); err != nil {
return nil, errors.Wrapf(err, "reconciling install resources failed")
}
meshIngress = createOrUpdateMeshIngress(meshIngress, install, meshRefs)
// caller should expect the install to have been modified
ref := meshIngress.Metadata.Ref()
installIngress.InstalledIngress = &ref
return meshIngress, nil
}
func createOrUpdateMeshIngress(meshIngress *v1.MeshIngress, install *v1.Install, meshRefs []*core.ResourceRef) *v1.MeshIngress {
if meshIngress != nil {
meshIngress.Meshes = meshRefs
meshIngress.InstallationNamespace = install.InstallationNamespace
meshIngress.MeshIngressType = &v1.MeshIngress_Gloo{
Gloo: &v1.GlooMeshIngress{},
}
return meshIngress
}
return &v1.MeshIngress{
Metadata: core.Metadata{
Namespace: install.Metadata.Namespace,
Name: install.Metadata.Name,
},
InstallationNamespace: install.InstallationNamespace,
MeshIngressType: &v1.MeshIngress_Gloo{
Gloo: &v1.GlooMeshIngress{},
},
Meshes: meshRefs,
}
}
func makeManifestsForInstall(ctx context.Context, install *v1.Install, gloo *v1.GlooInstall) (helmchart.Manifests, error) {
if install.InstallationNamespace == "" {
return nil, errors.Errorf("must provide installation namespace")
}
if gloo.Version == "" {
return nil, errors.Errorf("must provide gloo version")
}
manifests, err := helmchart.RenderManifests(ctx,
glooManifestUrl(gloo.Version),
helmValues,
"gloo", // release name used in some manifests for rendering
install.InstallationNamespace,
"", // use default kube version
)
if err != nil {
return nil, errors.Wrapf(err, "rendering install manifests")
}
return manifests, nil
}
func glooManifestUrl(version string) string {
var url = "https://storage.googleapis.com/solo-public-helm/charts/gloo-%s.tgz"
return fmt.Sprintf(url, version)
}