-
Notifications
You must be signed in to change notification settings - Fork 9
/
runtime.go
135 lines (107 loc) · 4.36 KB
/
runtime.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package kubernetes
import (
"context"
"fmt"
applycorev1 "k8s.io/client-go/applyconfigurations/core/v1"
"namespacelabs.dev/foundation/framework/kubernetes/kubedef"
"namespacelabs.dev/foundation/framework/kubernetes/kubetool"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/planning/planninghooks"
"namespacelabs.dev/foundation/internal/runtime"
"namespacelabs.dev/foundation/internal/runtime/kubernetes/client"
"namespacelabs.dev/foundation/internal/runtime/rtypes"
"namespacelabs.dev/foundation/schema"
runtimepb "namespacelabs.dev/foundation/schema/runtime"
"namespacelabs.dev/foundation/std/cfg"
"namespacelabs.dev/foundation/std/tasks"
)
var (
ObserveInitContainerLogs = false
kubernetesEnvConfigType = cfg.DefineConfigType[*kubetool.KubernetesEnv]()
)
const RestmapperStateKey = "kubernetes.restmapper"
type ProvideOverrideFunc func(context.Context, cfg.Configuration) (runtime.Class, error)
var classOverrides = map[string]ProvideOverrideFunc{}
func RegisterOverrideClass(name string, p ProvideOverrideFunc) {
classOverrides[name] = p
}
func Register() {
runtime.Register("kubernetes", func(ctx context.Context, cfg cfg.Configuration) (runtime.Class, error) {
hostEnv, err := client.CheckGetHostEnv(cfg)
if err != nil {
return nil, err
}
fmt.Fprintf(console.Debug(ctx), "kubernetes: selected %+v for %q\n", hostEnv, cfg.EnvKey())
tasks.TraceCaller(ctx, console.Debug, "kubernetes.New")
if hostEnv.Provider != "" {
if provider := classOverrides[hostEnv.Provider]; provider != nil {
klass, err := provider(ctx, cfg)
if err != nil {
return nil, err
}
if klass != nil {
return klass, nil
}
}
}
return kubernetesClass{}, nil
})
runtime.RegisterPrepare(RestmapperStateKey, func(ctx context.Context, _ cfg.Configuration, cluster runtime.Cluster) (any, error) {
kube, ok := cluster.(*Cluster)
if !ok {
return nil, fnerrors.InternalError("expected kubernetes cluster")
}
return client.NewRESTMapper(kube.RESTConfig(), kube.computedClient.Configuration.Ephemeral)
})
planninghooks.RegisterPrepareHook("namespacelabs.dev/foundation/std/runtime/kubernetes.ApplyServerExtensions", prepareApplyServerExtensions)
}
type kubernetesClass struct{}
var _ runtime.Class = kubernetesClass{}
func (d kubernetesClass) AttachToCluster(ctx context.Context, cfg cfg.Configuration) (runtime.Cluster, error) {
return ConnectToCluster(ctx, cfg)
}
func (d kubernetesClass) EnsureCluster(ctx context.Context, cfg cfg.Configuration, purpose string) (runtime.Cluster, error) {
return ConnectToCluster(ctx, cfg)
}
func newTarget(env cfg.Context) clusterTarget {
ns := ModuleNamespace(env.Workspace().Proto(), env.Environment())
if conf, ok := kubernetesEnvConfigType.CheckGet(env.Configuration()); ok {
ns = conf.Namespace
}
return clusterTarget{env: env.Environment(), namespace: ns}
}
func MakeNamespace(env *schema.Environment, ns string) *applycorev1.NamespaceApplyConfiguration {
return applycorev1.Namespace(ns).
WithLabels(kubedef.MakeLabels(env, nil)).
WithAnnotations(kubedef.MakeAnnotations(env, ""))
}
func PrepareProvisionWith(env *schema.Environment, ns string, systemInfo *kubedef.SystemInfo) (*rtypes.ProvisionProps, error) {
// Ensure the namespace exist, before we go and apply definitions to it. Also, deployServer
// assumes that a namespace already exists.
def, err := (kubedef.Apply{
Description: fmt.Sprintf("Namespace for %q", env.Name),
Resource: MakeNamespace(env, ns),
}).ToDefinition()
if err != nil {
return nil, err
}
// Pass the computed namespace to the provisioning tool.
return &rtypes.ProvisionProps{
ProvisionInput: []rtypes.ProvisionInput{
{Message: &kubetool.KubernetesEnv{Namespace: ns}},
{Message: systemInfo},
},
Invocation: []*schema.SerializedInvocation{def},
}, nil
}
func (r *Cluster) AttachTerminal(ctx context.Context, reference *runtimepb.ContainerReference, rio runtime.TerminalIO) error {
cpr := &kubedef.ContainerPodReference{}
if err := reference.Opaque.UnmarshalTo(cpr); err != nil {
return fnerrors.InternalError("invalid reference: %w", err)
}
return r.attachTerminal(ctx, r.cli, cpr, rio)
}