Mohammed Naser | 168acc3 | 2024-01-09 17:15:26 -0500 | [diff] [blame^] | 1 | // Copyright (c) 2024 VEXXHOST, Inc. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | // not use this file except in compliance with the License. You may obtain |
| 5 | // a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | // License for the specific language governing permissions and limitations |
| 13 | // under the License. |
| 14 | |
| 15 | package main |
| 16 | |
| 17 | import ( |
| 18 | "context" |
| 19 | "fmt" |
| 20 | "os" |
| 21 | |
| 22 | cmmeta "github.com/cert-manager/cert-manager/pkg/apis/meta/v1" |
| 23 | log "github.com/sirupsen/logrus" |
| 24 | "k8s.io/client-go/rest" |
| 25 | |
| 26 | "github.com/vexxhost/atmosphere/internal/tls" |
| 27 | ) |
| 28 | |
| 29 | const ( |
| 30 | EnvVarApiIssuerKind = "API_ISSUER_KIND" |
| 31 | EnvVarApiIssuerName = "API_ISSUER_NAME" |
| 32 | EnvVarVncIssuerKind = "VNC_ISSUER_KIND" |
| 33 | EnvVarVncIssuerName = "VNC_ISSUER_NAME" |
| 34 | ) |
| 35 | |
| 36 | func main() { |
| 37 | config, err := rest.InClusterConfig() |
| 38 | if err != nil { |
| 39 | log.Fatal(err) |
| 40 | } |
| 41 | |
| 42 | required := []string{ |
| 43 | EnvVarApiIssuerKind, |
| 44 | EnvVarApiIssuerName, |
| 45 | EnvVarVncIssuerKind, |
| 46 | EnvVarVncIssuerName, |
| 47 | } |
| 48 | |
| 49 | for _, env := range required { |
| 50 | if os.Getenv(env) == "" { |
| 51 | log.Fatal(fmt.Sprintf("missing required environment variable: %s", env)) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | ctx := context.Background() |
| 56 | go createCertificateSpec(ctx, config, tls.LibvirtCertificateTypeAPI) |
| 57 | go createCertificateSpec(ctx, config, tls.LibvirtCertificateTypeVNC) |
| 58 | |
| 59 | <-ctx.Done() |
| 60 | } |
| 61 | |
| 62 | func createCertificateSpec(ctx context.Context, config *rest.Config, certificateType tls.LibvirtCertificateType) { |
| 63 | var issuerRef cmmeta.ObjectReference |
| 64 | switch certificateType { |
| 65 | case tls.LibvirtCertificateTypeAPI: |
| 66 | issuerRef = cmmeta.ObjectReference{ |
| 67 | Kind: os.Getenv(EnvVarApiIssuerKind), |
| 68 | Name: os.Getenv(EnvVarApiIssuerName), |
| 69 | } |
| 70 | case tls.LibvirtCertificateTypeVNC: |
| 71 | issuerRef = cmmeta.ObjectReference{ |
| 72 | Kind: os.Getenv(EnvVarVncIssuerKind), |
| 73 | Name: os.Getenv(EnvVarVncIssuerName), |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | spec := &tls.LibvirtCertificateSpec{ |
| 78 | Type: certificateType, |
| 79 | IssuerRef: issuerRef, |
| 80 | } |
| 81 | |
| 82 | manager, err := tls.NewLibvirtManager(config, spec) |
| 83 | if err != nil { |
| 84 | log.Fatal(err) |
| 85 | } |
| 86 | |
| 87 | err = manager.Create(ctx) |
| 88 | if err != nil { |
| 89 | log.Fatal(err) |
| 90 | } |
| 91 | |
| 92 | log.WithFields(log.Fields{ |
| 93 | "certificateType": certificateType, |
| 94 | }).Info("certificate created") |
| 95 | |
| 96 | go manager.Watch(ctx) |
| 97 | } |