-
Notifications
You must be signed in to change notification settings - Fork 88
/
deploy.go
117 lines (100 loc) · 4.32 KB
/
deploy.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
package identity
import (
"context"
"fmt"
"log"
"github.com/pkg/errors"
identitydeploy "github.com/replicatedhq/kots/pkg/identity/deploy"
"github.com/replicatedhq/kots/pkg/ingress"
"github.com/replicatedhq/kots/pkg/k8sutil"
kotsadmtypes "github.com/replicatedhq/kots/pkg/kotsadm/types"
kotsadmversion "github.com/replicatedhq/kots/pkg/kotsadm/version"
"github.com/replicatedhq/kots/pkg/kurl"
kotsv1beta1 "github.com/replicatedhq/kotskinds/apis/kots/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
const (
KotsadmNamePrefix = "kotsadm"
)
func Deploy(
ctx context.Context,
clientset kubernetes.Interface,
namespace string,
identityConfig kotsv1beta1.IdentityConfig,
ingressConfig kotsv1beta1.IngressConfig,
registryConfig *kotsadmtypes.RegistryConfig,
proxyEnv map[string]string,
applyAppBranding bool,
) error {
identityConfig.Spec.ClientID = "kotsadm"
options := identitydeploy.Options{
NamePrefix: KotsadmNamePrefix,
Namespace: namespace,
IdentitySpec: getIdentitySpec(ctx, clientset, namespace, identityConfig.Spec, ingressConfig.Spec, applyAppBranding),
IdentityConfigSpec: identityConfig.Spec,
IsOpenShift: k8sutil.IsOpenShift(clientset),
ProxyEnv: proxyEnv,
Builder: nil,
}
isKurl, err := kurl.IsKurl(clientset)
if err != nil {
return errors.Wrap(err, "failed to check if cluster is kurl")
}
if !isKurl || namespace != metav1.NamespaceDefault {
options.ImageRewriteFn = kotsadmversion.DependencyImageRewriteKotsadmRegistry(namespace, registryConfig)
}
if err := migrateClientSecret(ctx, clientset, namespace); err != nil {
return errors.Wrap(err, "failed to migrate client secret")
}
return identitydeploy.Deploy(ctx, clientset, options)
}
func Configure(ctx context.Context, clientset kubernetes.Interface, namespace string, identityConfig kotsv1beta1.IdentityConfig, ingressConfig kotsv1beta1.IngressConfig, proxyEnv map[string]string, applyAppBranding bool) error {
identityConfig.Spec.ClientID = "kotsadm"
options := identitydeploy.Options{
NamePrefix: KotsadmNamePrefix,
Namespace: namespace,
IdentitySpec: getIdentitySpec(ctx, clientset, namespace, identityConfig.Spec, ingressConfig.Spec, applyAppBranding),
IdentityConfigSpec: identityConfig.Spec,
IsOpenShift: k8sutil.IsOpenShift(clientset),
ImageRewriteFn: nil,
ProxyEnv: proxyEnv,
Builder: nil,
}
return identitydeploy.Configure(ctx, clientset, options)
}
func Undeploy(ctx context.Context, clientset kubernetes.Interface, namespace string) error {
return identitydeploy.Undeploy(ctx, clientset, namespace, KotsadmNamePrefix)
}
func getIdentitySpec(ctx context.Context, clientset kubernetes.Interface, namespace string, identityConfigSpec kotsv1beta1.IdentityConfigSpec, ingressConfigSpec kotsv1beta1.IngressConfigSpec, applyAppBranding bool) kotsv1beta1.IdentitySpec {
// NOTE: when the user adds a second app the branding won't change
webConfig, err := getWebConfig(ctx, clientset, namespace, applyAppBranding)
if err != nil {
log.Printf("Failed to get branding: %v", err)
}
return kotsv1beta1.IdentitySpec{
IdentityIssuerURL: DexIssuerURL(identityConfigSpec),
OIDCRedirectURIs: []string{getRedirectURI(identityConfigSpec, ingressConfigSpec)},
OAUTH2AlwaysShowLoginScreen: true,
WebConfig: webConfig,
}
}
func getRedirectURI(identityConfigSpec kotsv1beta1.IdentityConfigSpec, ingressConfigSpec kotsv1beta1.IngressConfigSpec) string {
kotsadmAddress := identityConfigSpec.AdminConsoleAddress
if kotsadmAddress == "" && ingressConfigSpec.Enabled {
kotsadmAddress = ingress.GetAddress(ingressConfigSpec)
}
return fmt.Sprintf("%s/api/v1/oidc/login/callback", kotsadmAddress)
}
func migrateClientSecret(ctx context.Context, clientset kubernetes.Interface, namespace string) error {
client, err := getKotsadmOIDCClientFromDexConfig(ctx, clientset, namespace)
if err != nil {
return errors.Wrap(err, "failed to get existing oidc client from dex config")
}
if client == nil || client.Secret == "" {
return nil
}
secret := identitydeploy.ClientSecretResource(namespace, KotsadmNamePrefix, client.Secret, nil)
_, err = clientset.CoreV1().Secrets(namespace).Create(ctx, secret, metav1.CreateOptions{})
return errors.Wrap(err, "failed to create secret")
}