Skip to content

Commit

Permalink
Perform config parsing with sigs.k8s.io/yaml to reuse json tags (borc…
Browse files Browse the repository at this point in the history
  • Loading branch information
borchero authored Mar 11, 2023
1 parent bd7edd8 commit 8709c59
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 71 deletions.
12 changes: 8 additions & 4 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ version: 2
updates:
# Update GitHub actions
- directory: /
open-pull-requests-limit: 5
package-ecosystem: github-actions
schedule:
interval: weekly
interval: monthly
day: saturday
# Update Go dependencies
- directory: /
open-pull-requests-limit: 5
package-ecosystem: gomod
schedule:
interval: weekly
interval: monthly
day: saturday
# Update Docker base images
- directory: /
package-ecosystem: docker
schedule:
interval: monthly
day: saturday
36 changes: 7 additions & 29 deletions .github/workflows/application-build-test.yml
Original file line number Diff line number Diff line change
@@ -1,41 +1,19 @@
name: Build and Test Container Image
on:
release:
types:
- published
types: [published]
pull_request:
branches:
- main
branches: [main]
push:
branches:
- main
branches: [main]

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
file-changes:
name: Gather File Changes
runs-on: ubuntu-latest
outputs:
chart: ${{ steps.changes.outputs.chart }}
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Gather file changes
uses: dorny/paths-filter@v2
id: changes
with:
list-files: none
filters: |
chart:
- chart/**
build-image:
name: Build Image
needs:
- file-changes
runs-on: ubuntu-latest
steps:
- name: Checkout
Expand Down Expand Up @@ -75,24 +53,24 @@ jobs:
tags: ${{ steps.meta.outputs.tags }}
# Only export and upload the image if used for testing
- name: Export image for test platform
if: needs.file-changes.outputs.chart == 'true' && github.ref_type != 'tag'
if: github.ref_type != 'tag'
uses: docker/build-push-action@v4
with:
context: .
push: false
outputs: type=docker,dest=/tmp/image.tar
- name: Upload image for testing
uses: actions/upload-artifact@v3
if: needs.file-changes.outputs.chart == 'true' && github.ref_type != 'tag'
if: github.ref_type != 'tag'
with:
name: docker-image
path: /tmp/image.tar

e2e-tests:
name: Test Helm Chart
needs: [file-changes, build-image]
needs: build-image
runs-on: ubuntu-latest
if: needs.file-changes.outputs.chart == 'true' && github.ref_type != 'tag'
if: github.ref_type != 'tag'
steps:
- name: Checkout
uses: actions/checkout@v3
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/chart-publish.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
name: Publish Helm Chart
on:
release:
types:
- published
types: [published]

jobs:
publish:
Expand Down
7 changes: 5 additions & 2 deletions .github/workflows/ci-application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@ jobs:
lint:
name: Lint
runs-on: ubuntu-latest
container: golangci/golangci-lint:v1.50
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup Go
uses: actions/setup-go@v3
with:
go-version-file: go.mod
- name: Run lint
run: golangci-lint run ./...
uses: golangci/golangci-lint-action@v3

unit-test:
name: Unit Tests
Expand Down
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ linters:
enable:
- goimports
- revive
- stylecheck
issues:
include:
- EXC0002
Expand Down
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ repos:
rev: v1.51.2
hooks:
- id: golangci-lint
args: ["--go", "1.18"]
8 changes: 4 additions & 4 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
certmanager "github.com/jetstack/cert-manager/pkg/apis/certmanager/v1"
traefik "github.com/traefik/traefik/v2/pkg/provider/kubernetes/crd/traefik/v1alpha1"
"go.uber.org/zap"
"gopkg.in/yaml.v3"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand All @@ -21,6 +20,7 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/external-dns/endpoint"
"sigs.k8s.io/yaml"
)

func main() {
Expand All @@ -36,11 +36,11 @@ func main() {
// Load the config file if available
var config configv1.Config
if cfgFile != "" {
file, err := os.Open(cfgFile)
contents, err := os.ReadFile(cfgFile)
if err != nil {
logger.Fatal("failed to open config file", zap.Error(err))
logger.Fatal("failed to read config file", zap.Error(err))
}
if err := yaml.NewDecoder(file).Decode(&config); err != nil {
if err := yaml.Unmarshal(contents, &config); err != nil {
logger.Fatal("failed to parse config file", zap.Error(err))
}
}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ require (
github.com/stretchr/testify v1.8.2
github.com/traefik/traefik/v2 v2.9.8
go.uber.org/zap v1.24.0
gopkg.in/yaml.v3 v3.0.1
k8s.io/api v0.26.2
k8s.io/apimachinery v0.26.2
k8s.io/client-go v0.26.2
sigs.k8s.io/controller-runtime v0.14.5
sigs.k8s.io/external-dns v0.13.3
sigs.k8s.io/yaml v1.3.0
)

require (
Expand Down Expand Up @@ -82,14 +82,14 @@ require (
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apiextensions-apiserver v0.26.1 // indirect
k8s.io/component-base v0.26.1 // indirect
k8s.io/klog/v2 v2.80.1 // indirect
k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280 // indirect
k8s.io/utils v0.0.0-20221128185143-99ec85e7a448 // indirect
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)

replace (
Expand Down
44 changes: 19 additions & 25 deletions internal/config/v1/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,70 +6,64 @@ import (

// Config is the Schema for the configs API
type Config struct {
ControllerConfig `yaml:",inline"`
Selector IngressSelector `yaml:"selector"`
Integrations IntegrationConfigs `yaml:"integrations"`
ControllerConfig `json:",inline"`
Selector IngressSelector `json:"selector"`
Integrations IntegrationConfigs `json:"integrations"`
}

//-------------------------------------------------------------------------------------------------

// ControllerConfig provides configuration for the controller.
type ControllerConfig struct {
Health HealthConfig `yaml:"health,omitempty"`
LeaderElection LeaderElectionConfig `yaml:"leaderElection,omitempty"`
Metrics MetricsConfig `yaml:"metrics,omitempty"`
Health HealthConfig `json:"health,omitempty"`
LeaderElection LeaderElectionConfig `json:"leaderElection,omitempty"`
Metrics MetricsConfig `json:"metrics,omitempty"`
}

// HealthConfig provides configuration for the controller health checks.
type HealthConfig struct {
HealthProbeBindAddress string `yaml:"healthProbeBindAddress,omitempty"`
HealthProbeBindAddress string `json:"healthProbeBindAddress,omitempty"`
}

// LeaderElectionConfig provides configuration for the leader election.
type LeaderElectionConfig struct {
LeaderElect bool `yaml:"leaderElect,omitempty"`
ResourceName string `yaml:"resourceName,omitempty"`
ResourceNamespace string `yaml:"resourceNamespace,omitempty"`
LeaderElect bool `json:"leaderElect,omitempty"`
ResourceName string `json:"resourceName,omitempty"`
ResourceNamespace string `json:"resourceNamespace,omitempty"`
}

// MetricsConfig provides configuration for the controller metrics.
type MetricsConfig struct {
BindAddress string `yaml:"bindAddress,omitempty"`
BindAddress string `json:"bindAddress,omitempty"`
}

//-------------------------------------------------------------------------------------------------

// IngressSelector can be used to limit operations to ingresses with a specific class.
type IngressSelector struct {
IngressClass *string `yaml:"ingressClass,omitempty"`
IngressClass *string `json:"ingressClass,omitempty"`
}

// IntegrationConfigs describes the configurations for all integrations.
type IntegrationConfigs struct {
ExternalDNS *ExternalDNSIntegrationConfig `yaml:"externalDNS"`
CertManager *CertManagerIntegrationConfig `yaml:"certManager"`
ExternalDNS *ExternalDNSIntegrationConfig `json:"externalDNS"`
CertManager *CertManagerIntegrationConfig `json:"certManager"`
}

// ExternalDNSIntegrationConfig describes the configuration for the external-dns integration.
// Exactly one of target and target IPs should be set.
type ExternalDNSIntegrationConfig struct {
TargetService *ServiceRef `yaml:"targetService,omitempty"`
TargetIPs []string `yaml:"targetIPs,omitempty"`
TargetService *ServiceRef `json:"targetService,omitempty"`
TargetIPs []string `json:"targetIPs,omitempty"`
}

// CertManagerIntegrationConfig describes the configuration for the cert-manager integration.
type CertManagerIntegrationConfig struct {
Template v1.Certificate `yaml:"certificateTemplate"`
Template v1.Certificate `json:"certificateTemplate"`
}

// ServiceRef uniquely describes a Kubernetes service.
type ServiceRef struct {
Name string `yaml:"name"`
Namespace string `yaml:"namespace"`
}

// IssuerRef uniquely references a cert-manager issuer.
type IssuerRef struct {
Kind string `yaml:"kind"`
Name string `yaml:"name"`
Name string `json:"name"`
Namespace string `json:"namespace"`
}
5 changes: 2 additions & 3 deletions internal/integrations/certmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,19 @@ import (
"github.com/borchero/switchboard/internal/k8s"
"github.com/imdario/mergo"
certmanager "github.com/jetstack/cert-manager/pkg/apis/certmanager/v1"
v1 "github.com/jetstack/cert-manager/pkg/apis/certmanager/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)

type certManager struct {
client client.Client
template v1.Certificate
template certmanager.Certificate
}

// NewCertManager initializes a new cert-manager integration which creates certificates which use
// the provided issuer.
func NewCertManager(client client.Client, template v1.Certificate) Integration {
func NewCertManager(client client.Client, template certmanager.Certificate) Integration {
return &certManager{client, template}
}

Expand Down

0 comments on commit 8709c59

Please sign in to comment.