Skip to content

Commit 66e932c

Browse files
Kamil PrzybylKamil Przybyl
authored andcommitted
feat: read configuration from cloud config
1 parent e07f91b commit 66e932c

File tree

1 file changed

+57
-24
lines changed
  • cmd/application-load-balancer-controller-manager

1 file changed

+57
-24
lines changed

cmd/application-load-balancer-controller-manager/main.go

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,18 @@ import (
2020
"crypto/tls"
2121
"flag"
2222
"fmt"
23+
"io"
2324
"os"
2425
"path/filepath"
2526

2627
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
2728
// to ensure that exec-entrypoint and run can make use of them.
28-
_ "k8s.io/client-go/plugin/pkg/client/auth"
29-
3029
sdkconfig "github.com/stackitcloud/stackit-sdk-go/core/config"
30+
"gopkg.in/yaml.v3"
3131
"k8s.io/apimachinery/pkg/runtime"
3232
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3333
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
34+
_ "k8s.io/client-go/plugin/pkg/client/auth"
3435
ctrl "sigs.k8s.io/controller-runtime"
3536
"sigs.k8s.io/controller-runtime/pkg/certwatcher"
3637
"sigs.k8s.io/controller-runtime/pkg/healthz"
@@ -40,7 +41,7 @@ import (
4041
"sigs.k8s.io/controller-runtime/pkg/webhook"
4142

4243
"github.com/stackitcloud/cloud-provider-stackit/pkg/alb/ingress"
43-
"github.com/stackitcloud/cloud-provider-stackit/pkg/stackit"
44+
albclient "github.com/stackitcloud/cloud-provider-stackit/pkg/stackit"
4445
albsdk "github.com/stackitcloud/stackit-sdk-go/services/alb/v2api"
4546
certsdk "github.com/stackitcloud/stackit-sdk-go/services/certificates/v2api"
4647
// +kubebuilder:scaffold:imports
@@ -57,6 +58,49 @@ func init() {
5758
// +kubebuilder:scaffold:scheme
5859
}
5960

61+
type Config struct {
62+
NetworkID string `yaml:"networkID"`
63+
ProjectID string `yaml:"projectID"`
64+
Region string `yaml:"region"`
65+
}
66+
67+
// ReadConfig reads the ALB infrastructure configuration provided via the cloud-config flag.
68+
func ReadConfig(cloudConfig string) Config {
69+
configFile, err := os.Open(cloudConfig)
70+
if err != nil {
71+
setupLog.Error(err, "Failed to open the cloud config file")
72+
os.Exit(1)
73+
}
74+
defer configFile.Close()
75+
76+
var config Config
77+
content, err := io.ReadAll(configFile)
78+
if err != nil {
79+
setupLog.Error(err, "Failed to read config content")
80+
os.Exit(1)
81+
}
82+
83+
err = yaml.Unmarshal(content, &config)
84+
if err != nil {
85+
setupLog.Error(err, "Failed to parse config as YAML")
86+
os.Exit(1)
87+
}
88+
89+
if config.ProjectID == "" {
90+
setupLog.Error(err, "projectId must be set")
91+
os.Exit(1)
92+
}
93+
if config.Region == "" {
94+
setupLog.Error(err, "region must be set")
95+
os.Exit(1)
96+
}
97+
if config.NetworkID == "" {
98+
setupLog.Error(err, "networkId must be set")
99+
os.Exit(1)
100+
}
101+
return config
102+
}
103+
60104
// nolint:gocyclo,funlen // TODO: Refactor into smaller functions.
61105
func main() {
62106
var metricsAddr string
@@ -66,6 +110,7 @@ func main() {
66110
var leaderElectionNamespace string
67111
var leaderElectionID string
68112
var probeAddr string
113+
var cloudConfig string
69114
var secureMetrics bool
70115
var enableHTTP2 bool
71116
var tlsOpts []func(*tls.Config)
@@ -90,6 +135,7 @@ func main() {
90135
flag.StringVar(&metricsCertKey, "metrics-cert-key", "tls.key", "The name of the metrics server key file.")
91136
flag.BoolVar(&enableHTTP2, "enable-http2", false,
92137
"If set, HTTP/2 will be enabled for the metrics and webhook servers")
138+
flag.StringVar(&cloudConfig, "cloud-config", "cloud.yaml", "The path to the cloud config file.")
93139
opts := zap.Options{
94140
Development: true,
95141
}
@@ -98,6 +144,8 @@ func main() {
98144

99145
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
100146

147+
config := ReadConfig(cloudConfig)
148+
101149
// if the enable-http2 flag is false (the default), http/2 should be disabled
102150
// due to its vulnerabilities. More specifically, disabling http/2 will
103151
// prevent from being vulnerable to the HTTP/2 Stream Cancellation and
@@ -216,24 +264,9 @@ func main() {
216264

217265
certURL, _ := os.LookupEnv("STACKIT_LOAD_BALANCER_API_CERT_URL")
218266

219-
region, set := os.LookupEnv("STACKIT_REGION")
220-
if !set {
221-
setupLog.Error(err, "STACKIT_REGION not set", "controller", "IngressClass")
222-
os.Exit(1)
223-
}
224-
projectID, set := os.LookupEnv("PROJECT_ID")
225-
if !set {
226-
setupLog.Error(err, "PROJECT_ID not set", "controller", "IngressClass")
227-
os.Exit(1)
228-
}
229-
networkID, set := os.LookupEnv("NETWORK_ID")
230-
if !set {
231-
setupLog.Error(err, "NETWORK_ID not set", "controller", "IngressClass")
232-
os.Exit(1)
233-
}
234-
235267
// Create an ALB SDK client
236268
albOpts := []sdkconfig.ConfigurationOption{}
269+
237270
if albURL != "" {
238271
albOpts = append(albOpts, sdkconfig.WithEndpoint(albURL))
239272
}
@@ -251,7 +284,7 @@ func main() {
251284
}
252285
// Create an ALB client
253286
fmt.Printf("Create ALB client\n")
254-
albClient, err := stackit.NewApplicationLoadBalancerClient(sdkClient)
287+
albClient, err := albclient.NewApplicationLoadBalancerClient(sdkClient)
255288
if err != nil {
256289
setupLog.Error(err, "unable to create ALB client", "controller", "IngressClass")
257290
os.Exit(1)
@@ -264,7 +297,7 @@ func main() {
264297
os.Exit(1)
265298
}
266299
// Create an Certificates API client
267-
certificateClient, err := stackit.NewCertClient(certificateAPI)
300+
certificateClient, err := albclient.NewCertClient(certificateAPI)
268301
if err != nil {
269302
setupLog.Error(err, "unable to create Certificates client", "controller", "IngressClass")
270303
os.Exit(1)
@@ -275,9 +308,9 @@ func main() {
275308
ALBClient: albClient,
276309
CertificateClient: certificateClient,
277310
Scheme: mgr.GetScheme(),
278-
ProjectID: projectID,
279-
NetworkID: networkID,
280-
Region: region,
311+
ProjectID: config.ProjectID,
312+
NetworkID: config.NetworkID,
313+
Region: config.Region,
281314
}).SetupWithManager(mgr); err != nil {
282315
setupLog.Error(err, "unable to create controller", "controller", "IngressClass")
283316
os.Exit(1)

0 commit comments

Comments
 (0)