/
setup.go
129 lines (109 loc) · 4.37 KB
/
setup.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
/*
Copyright 2024 IBM Corporation.
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 controller
import (
"context"
"errors"
"fmt"
"net/http"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/healthz"
cert "github.com/open-policy-agent/cert-controller/pkg/rotator"
"github.com/project-codeflare/appwrapper/internal/controller/appwrapper"
"github.com/project-codeflare/appwrapper/internal/controller/workload"
"github.com/project-codeflare/appwrapper/internal/webhook"
"github.com/project-codeflare/appwrapper/pkg/config"
"sigs.k8s.io/kueue/pkg/controller/jobframework"
)
// SetupControllers creates and configures all components of the AppWrapper controller
func SetupControllers(mgr ctrl.Manager, awConfig *config.AppWrapperConfig) error {
if awConfig.EnableKueueIntegrations {
if err := workload.WorkloadReconciler(
mgr.GetClient(),
mgr.GetEventRecorderFor("kueue"),
jobframework.WithManageJobsWithoutQueueName(awConfig.ManageJobsWithoutQueueName),
).SetupWithManager(mgr); err != nil {
return fmt.Errorf("workload controller: %w", err)
}
if err := (&workload.ChildWorkloadReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
return fmt.Errorf("child admission controller: %w", err)
}
}
if err := (&appwrapper.AppWrapperReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Config: awConfig,
}).SetupWithManager(mgr); err != nil {
return fmt.Errorf("appwrapper controller: %w", err)
}
return nil
}
// SetupWebhooks creates and configures the AppWrapper controller's Webhooks
func SetupWebhooks(mgr ctrl.Manager, awConfig *config.AppWrapperConfig) error {
if err := (&webhook.AppWrapperWebhook{
Config: awConfig,
}).SetupWebhookWithManager(mgr); err != nil {
return fmt.Errorf("webhook: %w", err)
}
return nil
}
func SetupIndexers(ctx context.Context, mgr ctrl.Manager, awConfig *config.AppWrapperConfig) error {
if awConfig.EnableKueueIntegrations {
if err := jobframework.SetupWorkloadOwnerIndex(ctx, mgr.GetFieldIndexer(), workload.GVK); err != nil {
return fmt.Errorf("workload indexer: %w", err)
}
}
return nil
}
func SetupProbeEndpoints(mgr ctrl.Manager, certsReady chan struct{}) error {
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
return fmt.Errorf("health check: %w", err)
}
if err := mgr.AddReadyzCheck("readyz", func(req *http.Request) error {
select {
case <-certsReady:
return mgr.GetWebhookServer().StartedChecker()(req)
default:
return errors.New("certificates are not ready")
}
}); err != nil {
return fmt.Errorf("readiness check: %w", err)
}
return nil
}
// +kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch;update
// +kubebuilder:rbac:groups="admissionregistration.k8s.io",resources=mutatingwebhookconfigurations,verbs=get;list;watch;update
// +kubebuilder:rbac:groups="admissionregistration.k8s.io",resources=validatingwebhookconfigurations,verbs=get;list;watch;update
func SetupCertManagement(mgr ctrl.Manager, config *config.CertManagementConfig, certsReady chan struct{}) error {
// DNSName is <service name>.<namespace>.svc
var dnsName = fmt.Sprintf("%s.%s.svc", config.WebhookServiceName, config.Namespace)
return cert.AddRotator(mgr, &cert.CertRotator{
SecretKey: types.NamespacedName{Namespace: config.Namespace, Name: config.WebhookSecretName},
CertDir: config.CertificateDir,
CAName: config.CertificateName,
CAOrganization: config.CertificateOrg,
DNSName: dnsName,
IsReady: certsReady,
Webhooks: []cert.WebhookInfo{
{Type: cert.Validating, Name: config.ValidatingWebhookConfigName},
{Type: cert.Mutating, Name: config.MutatingWebhookConfigName},
},
// When the controller is running in the leader election mode,
// we expect webhook server will run in primary and secondary instance
RequireLeaderElection: false,
})
}