Skip to content

Commit 8709c59

Browse files
authored
Perform config parsing with sigs.k8s.io/yaml to reuse json tags (#86)
1 parent bd7edd8 commit 8709c59

10 files changed

+50
-71
lines changed

.github/dependabot.yml

+8-4
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@ version: 2
22
updates:
33
# Update GitHub actions
44
- directory: /
5-
open-pull-requests-limit: 5
65
package-ecosystem: github-actions
76
schedule:
8-
interval: weekly
7+
interval: monthly
98
day: saturday
109
# Update Go dependencies
1110
- directory: /
12-
open-pull-requests-limit: 5
1311
package-ecosystem: gomod
1412
schedule:
15-
interval: weekly
13+
interval: monthly
14+
day: saturday
15+
# Update Docker base images
16+
- directory: /
17+
package-ecosystem: docker
18+
schedule:
19+
interval: monthly
1620
day: saturday

.github/workflows/application-build-test.yml

+7-29
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,19 @@
11
name: Build and Test Container Image
22
on:
33
release:
4-
types:
5-
- published
4+
types: [published]
65
pull_request:
7-
branches:
8-
- main
6+
branches: [main]
97
push:
10-
branches:
11-
- main
8+
branches: [main]
129

1310
concurrency:
1411
group: ${{ github.workflow }}-${{ github.ref }}
1512
cancel-in-progress: true
1613

1714
jobs:
18-
file-changes:
19-
name: Gather File Changes
20-
runs-on: ubuntu-latest
21-
outputs:
22-
chart: ${{ steps.changes.outputs.chart }}
23-
steps:
24-
- name: Checkout
25-
uses: actions/checkout@v3
26-
- name: Gather file changes
27-
uses: dorny/paths-filter@v2
28-
id: changes
29-
with:
30-
list-files: none
31-
filters: |
32-
chart:
33-
- chart/**
34-
3515
build-image:
3616
name: Build Image
37-
needs:
38-
- file-changes
3917
runs-on: ubuntu-latest
4018
steps:
4119
- name: Checkout
@@ -75,24 +53,24 @@ jobs:
7553
tags: ${{ steps.meta.outputs.tags }}
7654
# Only export and upload the image if used for testing
7755
- name: Export image for test platform
78-
if: needs.file-changes.outputs.chart == 'true' && github.ref_type != 'tag'
56+
if: github.ref_type != 'tag'
7957
uses: docker/build-push-action@v4
8058
with:
8159
context: .
8260
push: false
8361
outputs: type=docker,dest=/tmp/image.tar
8462
- name: Upload image for testing
8563
uses: actions/upload-artifact@v3
86-
if: needs.file-changes.outputs.chart == 'true' && github.ref_type != 'tag'
64+
if: github.ref_type != 'tag'
8765
with:
8866
name: docker-image
8967
path: /tmp/image.tar
9068

9169
e2e-tests:
9270
name: Test Helm Chart
93-
needs: [file-changes, build-image]
71+
needs: build-image
9472
runs-on: ubuntu-latest
95-
if: needs.file-changes.outputs.chart == 'true' && github.ref_type != 'tag'
73+
if: github.ref_type != 'tag'
9674
steps:
9775
- name: Checkout
9876
uses: actions/checkout@v3

.github/workflows/chart-publish.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
name: Publish Helm Chart
22
on:
33
release:
4-
types:
5-
- published
4+
types: [published]
65

76
jobs:
87
publish:

.github/workflows/ci-application.yml

+5-2
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@ jobs:
1717
lint:
1818
name: Lint
1919
runs-on: ubuntu-latest
20-
container: golangci/golangci-lint:v1.50
2120
steps:
2221
- name: Checkout
2322
uses: actions/checkout@v3
23+
- name: Setup Go
24+
uses: actions/setup-go@v3
25+
with:
26+
go-version-file: go.mod
2427
- name: Run lint
25-
run: golangci-lint run ./...
28+
uses: golangci/golangci-lint-action@v3
2629

2730
unit-test:
2831
name: Unit Tests

.golangci.yml

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ linters:
44
enable:
55
- goimports
66
- revive
7+
- stylecheck
78
issues:
89
include:
910
- EXC0002

.pre-commit-config.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ repos:
1313
rev: v1.51.2
1414
hooks:
1515
- id: golangci-lint
16+
args: ["--go", "1.18"]

cmd/main.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
certmanager "github.com/jetstack/cert-manager/pkg/apis/certmanager/v1"
1212
traefik "github.com/traefik/traefik/v2/pkg/provider/kubernetes/crd/traefik/v1alpha1"
1313
"go.uber.org/zap"
14-
"gopkg.in/yaml.v3"
1514
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1615
"k8s.io/apimachinery/pkg/runtime"
1716
"k8s.io/apimachinery/pkg/runtime/schema"
@@ -21,6 +20,7 @@ import (
2120
ctrl "sigs.k8s.io/controller-runtime"
2221
"sigs.k8s.io/controller-runtime/pkg/healthz"
2322
"sigs.k8s.io/external-dns/endpoint"
23+
"sigs.k8s.io/yaml"
2424
)
2525

2626
func main() {
@@ -36,11 +36,11 @@ func main() {
3636
// Load the config file if available
3737
var config configv1.Config
3838
if cfgFile != "" {
39-
file, err := os.Open(cfgFile)
39+
contents, err := os.ReadFile(cfgFile)
4040
if err != nil {
41-
logger.Fatal("failed to open config file", zap.Error(err))
41+
logger.Fatal("failed to read config file", zap.Error(err))
4242
}
43-
if err := yaml.NewDecoder(file).Decode(&config); err != nil {
43+
if err := yaml.Unmarshal(contents, &config); err != nil {
4444
logger.Fatal("failed to parse config file", zap.Error(err))
4545
}
4646
}

go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ require (
1111
github.com/stretchr/testify v1.8.2
1212
github.com/traefik/traefik/v2 v2.9.8
1313
go.uber.org/zap v1.24.0
14-
gopkg.in/yaml.v3 v3.0.1
1514
k8s.io/api v0.26.2
1615
k8s.io/apimachinery v0.26.2
1716
k8s.io/client-go v0.26.2
1817
sigs.k8s.io/controller-runtime v0.14.5
1918
sigs.k8s.io/external-dns v0.13.3
19+
sigs.k8s.io/yaml v1.3.0
2020
)
2121

2222
require (
@@ -82,14 +82,14 @@ require (
8282
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
8383
gopkg.in/inf.v0 v0.9.1 // indirect
8484
gopkg.in/yaml.v2 v2.4.0 // indirect
85+
gopkg.in/yaml.v3 v3.0.1 // indirect
8586
k8s.io/apiextensions-apiserver v0.26.1 // indirect
8687
k8s.io/component-base v0.26.1 // indirect
8788
k8s.io/klog/v2 v2.80.1 // indirect
8889
k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280 // indirect
8990
k8s.io/utils v0.0.0-20221128185143-99ec85e7a448 // indirect
9091
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect
9192
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
92-
sigs.k8s.io/yaml v1.3.0 // indirect
9393
)
9494

9595
replace (

internal/config/v1/config.go

+19-25
Original file line numberDiff line numberDiff line change
@@ -6,70 +6,64 @@ import (
66

77
// Config is the Schema for the configs API
88
type Config struct {
9-
ControllerConfig `yaml:",inline"`
10-
Selector IngressSelector `yaml:"selector"`
11-
Integrations IntegrationConfigs `yaml:"integrations"`
9+
ControllerConfig `json:",inline"`
10+
Selector IngressSelector `json:"selector"`
11+
Integrations IntegrationConfigs `json:"integrations"`
1212
}
1313

1414
//-------------------------------------------------------------------------------------------------
1515

1616
// ControllerConfig provides configuration for the controller.
1717
type ControllerConfig struct {
18-
Health HealthConfig `yaml:"health,omitempty"`
19-
LeaderElection LeaderElectionConfig `yaml:"leaderElection,omitempty"`
20-
Metrics MetricsConfig `yaml:"metrics,omitempty"`
18+
Health HealthConfig `json:"health,omitempty"`
19+
LeaderElection LeaderElectionConfig `json:"leaderElection,omitempty"`
20+
Metrics MetricsConfig `json:"metrics,omitempty"`
2121
}
2222

2323
// HealthConfig provides configuration for the controller health checks.
2424
type HealthConfig struct {
25-
HealthProbeBindAddress string `yaml:"healthProbeBindAddress,omitempty"`
25+
HealthProbeBindAddress string `json:"healthProbeBindAddress,omitempty"`
2626
}
2727

2828
// LeaderElectionConfig provides configuration for the leader election.
2929
type LeaderElectionConfig struct {
30-
LeaderElect bool `yaml:"leaderElect,omitempty"`
31-
ResourceName string `yaml:"resourceName,omitempty"`
32-
ResourceNamespace string `yaml:"resourceNamespace,omitempty"`
30+
LeaderElect bool `json:"leaderElect,omitempty"`
31+
ResourceName string `json:"resourceName,omitempty"`
32+
ResourceNamespace string `json:"resourceNamespace,omitempty"`
3333
}
3434

3535
// MetricsConfig provides configuration for the controller metrics.
3636
type MetricsConfig struct {
37-
BindAddress string `yaml:"bindAddress,omitempty"`
37+
BindAddress string `json:"bindAddress,omitempty"`
3838
}
3939

4040
//-------------------------------------------------------------------------------------------------
4141

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

4747
// IntegrationConfigs describes the configurations for all integrations.
4848
type IntegrationConfigs struct {
49-
ExternalDNS *ExternalDNSIntegrationConfig `yaml:"externalDNS"`
50-
CertManager *CertManagerIntegrationConfig `yaml:"certManager"`
49+
ExternalDNS *ExternalDNSIntegrationConfig `json:"externalDNS"`
50+
CertManager *CertManagerIntegrationConfig `json:"certManager"`
5151
}
5252

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

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

6565
// ServiceRef uniquely describes a Kubernetes service.
6666
type ServiceRef struct {
67-
Name string `yaml:"name"`
68-
Namespace string `yaml:"namespace"`
69-
}
70-
71-
// IssuerRef uniquely references a cert-manager issuer.
72-
type IssuerRef struct {
73-
Kind string `yaml:"kind"`
74-
Name string `yaml:"name"`
67+
Name string `json:"name"`
68+
Namespace string `json:"namespace"`
7569
}

internal/integrations/certmanager.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,19 @@ import (
77
"github.com/borchero/switchboard/internal/k8s"
88
"github.com/imdario/mergo"
99
certmanager "github.com/jetstack/cert-manager/pkg/apis/certmanager/v1"
10-
v1 "github.com/jetstack/cert-manager/pkg/apis/certmanager/v1"
1110
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1211
"sigs.k8s.io/controller-runtime/pkg/client"
1312
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
1413
)
1514

1615
type certManager struct {
1716
client client.Client
18-
template v1.Certificate
17+
template certmanager.Certificate
1918
}
2019

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

0 commit comments

Comments
 (0)