This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
188 lines (169 loc) · 5.75 KB
/
main.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main
import (
"context"
"fmt"
"log"
"net/http"
"net/url"
"os"
"time"
"github.com/pivotal/kpack/pkg/registry"
k8sclient "k8s.io/client-go/kubernetes"
"code.cloudfoundry.org/cf-k8s-api/apis"
"code.cloudfoundry.org/cf-k8s-api/config"
"code.cloudfoundry.org/cf-k8s-api/payloads"
"code.cloudfoundry.org/cf-k8s-api/repositories"
"code.cloudfoundry.org/cf-k8s-api/repositories/authorization"
"code.cloudfoundry.org/cf-k8s-api/repositories/provider"
networkingv1alpha1 "code.cloudfoundry.org/cf-k8s-controllers/apis/networking/v1alpha1"
workloadsv1alpha1 "code.cloudfoundry.org/cf-k8s-controllers/apis/workloads/v1alpha1"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/gorilla/mux"
"github.com/pivotal/kpack/pkg/dockercreds/k8sdockercreds"
corev1 "k8s.io/api/core/v1"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
hnsv1alpha2 "sigs.k8s.io/hierarchical-namespaces/api/v1alpha2"
)
var createTimeout = time.Second * 30
func init() {
utilruntime.Must(workloadsv1alpha1.AddToScheme(scheme.Scheme))
utilruntime.Must(networkingv1alpha1.AddToScheme(scheme.Scheme))
utilruntime.Must(hnsv1alpha2.AddToScheme(scheme.Scheme))
}
type APIHandler interface {
RegisterRoutes(router *mux.Router)
}
func main() {
configPath, found := os.LookupEnv("CONFIG")
if !found {
panic("CONFIG must be set")
}
config, err := config.LoadFromPath(configPath)
if err != nil {
errorMessage := fmt.Sprintf("Config could not be read: %v", err)
panic(errorMessage)
}
payloads.DefaultLifecycleConfig = config.DefaultLifecycleConfig
k8sClientConfig := ctrl.GetConfigOrDie()
zapOpts := zap.Options{
// TODO: this needs to be configurable
Development: true,
}
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&zapOpts)))
privilegedCRClient, err := client.NewWithWatch(k8sClientConfig, client.Options{})
if err != nil {
panic(fmt.Sprintf("could not create privileged k8s client: %v", err))
}
privilegedK8sClient, err := repositories.BuildK8sClient(k8sClientConfig)
if err != nil {
panic(fmt.Sprintf("could not create privileged k8s client: %v", err))
}
serverURL, err := url.Parse(config.ServerURL)
if err != nil {
panic(fmt.Sprintf("could not parse server URL: %v", err))
}
orgRepo := repositories.NewOrgRepo(config.RootNamespace, privilegedCRClient, createTimeout)
handlers := []APIHandler{
apis.NewRootV3Handler(config.ServerURL),
apis.NewRootHandler(
ctrl.Log.WithName("RootHandler"),
config.ServerURL,
),
apis.NewResourceMatchesHandler(config.ServerURL),
apis.NewAppHandler(
ctrl.Log.WithName("AppHandler"),
*serverURL,
new(repositories.AppRepo),
new(repositories.DropletRepo),
new(repositories.ProcessRepository),
new(repositories.RouteRepo),
new(repositories.DomainRepo),
repositories.BuildCRClient,
k8sClientConfig,
),
apis.NewRouteHandler(
ctrl.Log.WithName("RouteHandler"),
*serverURL,
new(repositories.RouteRepo),
new(repositories.DomainRepo),
new(repositories.AppRepo),
repositories.BuildCRClient,
k8sClientConfig,
),
apis.NewPackageHandler(
ctrl.Log.WithName("PackageHandler"),
*serverURL,
new(repositories.PackageRepo),
new(repositories.AppRepo),
repositories.BuildCRClient,
repositories.UploadSourceImage,
newRegistryAuthBuilder(privilegedK8sClient, config),
k8sClientConfig,
config.PackageRegistryBase,
config.PackageRegistrySecretName,
),
apis.NewBuildHandler(
ctrl.Log.WithName("BuildHandler"),
*serverURL,
new(repositories.BuildRepo),
new(repositories.PackageRepo),
repositories.BuildCRClient,
k8sClientConfig,
),
apis.NewDropletHandler(
ctrl.Log.WithName("DropletHandler"),
*serverURL,
new(repositories.DropletRepo),
repositories.BuildCRClient,
k8sClientConfig,
),
apis.NewProcessHandler(
ctrl.Log.WithName("ProcessHandler"),
*serverURL,
new(repositories.ProcessRepository),
repositories.BuildCRClient,
k8sClientConfig,
),
wireOrgHandler(*serverURL, orgRepo, privilegedCRClient, config.AuthEnabled),
apis.NewSpaceHandler(
repositories.NewOrgRepo(config.RootNamespace, privilegedCRClient, createTimeout),
*serverURL,
),
}
router := mux.NewRouter()
for _, handler := range handlers {
handler.RegisterRoutes(router)
}
portString := fmt.Sprintf(":%v", config.ServerPort)
log.Fatal(http.ListenAndServe(portString, router))
}
func newRegistryAuthBuilder(privilegedK8sClient k8sclient.Interface, config *config.Config) func(ctx context.Context) (remote.Option, error) {
return func(ctx context.Context) (remote.Option, error) {
keychainFactory, err := k8sdockercreds.NewSecretKeychainFactory(privilegedK8sClient)
if err != nil {
return nil, fmt.Errorf("error in k8sdockercreds.NewSecretKeychainFactory: %w", err)
}
keychain, err := keychainFactory.KeychainForSecretRef(ctx, registry.SecretRef{
Namespace: config.RootNamespace,
ImagePullSecrets: []corev1.LocalObjectReference{{Name: config.PackageRegistrySecretName}},
})
if err != nil {
return nil, fmt.Errorf("error in keychainFactory.KeychainForSecretRef: %w", err)
}
return remote.WithAuthFromKeychain(keychain), nil
}
}
func wireOrgHandler(serverUrl url.URL, orgRepo *repositories.OrgRepo, client client.Client, authEnabled bool) *apis.OrgHandler {
var orgRepoProvider apis.OrgRepositoryProvider = provider.NewPrivilegedOrg(orgRepo)
if authEnabled {
authNsProvider := authorization.NewOrg(client)
tokenReviewer := authorization.NewTokenReviewer(client)
identityProvider := authorization.NewIdentityProvider(tokenReviewer)
orgRepoProvider = provider.NewOrg(orgRepo, authNsProvider, identityProvider)
}
return apis.NewOrgHandler(serverUrl, orgRepoProvider)
}