Skip to content

Commit d183c2e

Browse files
committed
chore: create personal organizations
1 parent b2f5b06 commit d183c2e

15 files changed

Lines changed: 720 additions & 125 deletions

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,6 @@ go.work
2525
*.swp
2626
*.swo
2727
*~
28+
29+
# Ignore kubeconfig
30+
.kube

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ RUN go mod download
1313

1414
# Copy the go source
1515
COPY cmd/ cmd/
16+
COPY internal/ internal/
1617

1718
# Build
1819
# the GOARCH has not a default value to allow the binary be built according to the host where the command

Makefile

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and Cust
4646
$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
4747

4848
.PHONY: generate
49-
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
49+
generate: controller-gen defaulter-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
5050
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
51+
$(DEFAULTER_GEN) ./internal/config --output-file=zz_generated.defaults.go
5152

5253
.PHONY: fmt
5354
fmt: ## Run go fmt against code.
@@ -168,12 +169,14 @@ KUBECTL ?= kubectl
168169
KIND ?= kind
169170
KUSTOMIZE ?= $(LOCALBIN)/kustomize
170171
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
172+
DEFAULTER_GEN ?= $(LOCALBIN)/defaulter-gen
171173
ENVTEST ?= $(LOCALBIN)/setup-envtest
172174
GOLANGCI_LINT = $(LOCALBIN)/golangci-lint
173175

174176
## Tool Versions
175177
KUSTOMIZE_VERSION ?= v5.6.0
176178
CONTROLLER_TOOLS_VERSION ?= v0.17.2
179+
DEFAULTER_GEN_VERSION ?= v0.32.3
177180
#ENVTEST_VERSION is the version of controller-runtime release branch to fetch the envtest setup script (i.e. release-0.20)
178181
ENVTEST_VERSION ?= $(shell go list -m -f "{{ .Version }}" sigs.k8s.io/controller-runtime | awk -F'[v.]' '{printf "release-%d.%d", $$2, $$3}')
179182
#ENVTEST_K8S_VERSION is the version of Kubernetes to use for setting up ENVTEST binaries (i.e. 1.31)
@@ -190,6 +193,11 @@ controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessar
190193
$(CONTROLLER_GEN): $(LOCALBIN)
191194
$(call go-install-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/controller-gen,$(CONTROLLER_TOOLS_VERSION))
192195

196+
.PHONY: defaulter-gen
197+
defaulter-gen: $(DEFAULTER_GEN) ## Download defaulter-gen locally if necessary.
198+
$(DEFAULTER_GEN): $(LOCALBIN)
199+
$(call go-install-tool,$(DEFAULTER_GEN),k8s.io/code-generator/cmd/defaulter-gen,$(DEFAULTER_GEN_VERSION))
200+
193201
.PHONY: setup-envtest
194202
setup-envtest: envtest ## Download the binaries required for ENVTEST in the local bin directory.
195203
@echo "Setting up envtest binaries for Kubernetes version $(ENVTEST_K8S_VERSION)..."

PROJECT

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,11 @@ layout:
77
- go.kubebuilder.io/v4
88
projectName: datum-cloud
99
repo: go.datum.net/datum
10+
resources:
11+
- controller: true
12+
domain: iam.datumapis.com
13+
external: true
14+
kind: User
15+
path: go.miloapis.com/milo/pkg/apis/iam/v1alpha1
16+
version: v1alpha1
1017
version: "3"

cmd/controller/manager.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ package controller
44
import (
55
"crypto/tls"
66
"flag"
7+
"fmt"
8+
"os"
79
"path/filepath"
810

911
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
1012
// to ensure that exec-entrypoint and run can make use of them.
1113
_ "k8s.io/client-go/plugin/pkg/client/auth"
1214

1315
"k8s.io/apimachinery/pkg/runtime"
16+
"k8s.io/apimachinery/pkg/runtime/serializer"
1417
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
1518
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
1619
ctrl "sigs.k8s.io/controller-runtime"
@@ -23,15 +26,22 @@ import (
2326

2427
"github.com/spf13/cobra"
2528
// +kubebuilder:scaffold:imports
29+
"go.datum.net/datum/internal/config"
30+
resourcemanagercontroller "go.datum.net/datum/internal/controller/resourcemanager"
31+
iamv1alpha1 "go.miloapis.com/milo/pkg/apis/iam/v1alpha1"
32+
resourcemanagerv1alpha1 "go.miloapis.com/milo/pkg/apis/resourcemanager/v1alpha1"
2633
)
2734

2835
var (
2936
scheme = runtime.NewScheme()
3037
setupLog = ctrl.Log.WithName("setup")
38+
codecs = serializer.NewCodecFactory(scheme, serializer.EnableStrict)
3139
)
3240

3341
func init() {
3442
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
43+
utilruntime.Must(iamv1alpha1.AddToScheme(scheme))
44+
utilruntime.Must(resourcemanagerv1alpha1.AddToScheme(scheme))
3545

3646
// +kubebuilder:scaffold:scheme
3747
}
@@ -45,6 +55,7 @@ func NewControllerManagerCommand() *cobra.Command {
4555
var probeAddr string
4656
var secureMetrics bool
4757
var enableHTTP2 bool
58+
var serverConfigFile string
4859

4960
cmd := &cobra.Command{
5061
Use: "controller-manager",
@@ -56,6 +67,7 @@ func NewControllerManagerCommand() *cobra.Command {
5667
metricsCertPath, metricsCertName, metricsCertKey,
5768
webhookCertPath, webhookCertName, webhookCertKey,
5869
enableLeaderElection,
70+
serverConfigFile,
5971
probeAddr,
6072
secureMetrics,
6173
enableHTTP2,
@@ -81,6 +93,7 @@ func NewControllerManagerCommand() *cobra.Command {
8193
cmd.Flags().StringVar(&metricsCertKey, "metrics-cert-key", "tls.key", "The name of the metrics server key file.")
8294
cmd.Flags().BoolVar(&enableHTTP2, "enable-http2", false,
8395
"If set, HTTP/2 will be enabled for the metrics and webhook servers")
96+
cmd.Flags().StringVar(&serverConfigFile, "config", "", "path to the controller manager config file")
8497

8598
// Add zap logging flags
8699
opts := zap.Options{
@@ -99,6 +112,7 @@ func runControllerManager(
99112
metricsCertPath, metricsCertName, metricsCertKey string,
100113
webhookCertPath, webhookCertName, webhookCertKey string,
101114
enableLeaderElection bool,
115+
serverConfigFile string,
102116
probeAddr string,
103117
secureMetrics bool,
104118
enableHTTP2 bool,
@@ -128,6 +142,21 @@ func runControllerManager(
128142
tlsOpts = append(tlsOpts, disableHTTP2)
129143
}
130144

145+
var serverConfig config.DatumControllerManager
146+
var configData []byte
147+
if len(serverConfigFile) > 0 {
148+
var err error
149+
configData, err = os.ReadFile(serverConfigFile)
150+
if err != nil {
151+
setupLog.Error(fmt.Errorf("unable to read server config from %q", serverConfigFile), "")
152+
os.Exit(1)
153+
}
154+
}
155+
156+
if err := runtime.DecodeInto(codecs.UniversalDecoder(), configData, &serverConfig); err != nil {
157+
return fmt.Errorf("unable to decode server config: %w", err)
158+
}
159+
131160
// Create watchers for metrics and webhooks certificates
132161
var metricsCertWatcher, webhookCertWatcher *certwatcher.CertWatcher
133162

@@ -226,6 +255,14 @@ func runControllerManager(
226255
return err
227256
}
228257

258+
if err = (&resourcemanagercontroller.PersonalOrganizationController{
259+
Client: mgr.GetClient(),
260+
Config: serverConfig.PersonalOrganizationController,
261+
}).SetupWithManager(mgr); err != nil {
262+
setupLog.Error(err, "unable to create controller", "controller", "PersonalOrganization")
263+
return err
264+
}
265+
229266
// +kubebuilder:scaffold:builder
230267

231268
if metricsCertWatcher != nil {

cmd/main.go

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@ package main
33

44
import (
55
"context"
6-
"fmt"
76
"os"
87

98
"github.com/spf13/cobra"
10-
"github.com/spf13/viper"
119

1210
"go.datum.net/datum/cmd/controller"
1311
)
@@ -37,42 +35,10 @@ func Execute() {
3735
}
3836

3937
func init() {
40-
cobra.OnInitialize(initConfig)
41-
42-
// Here you will define your flags and configuration settings.
43-
// Cobra supports persistent flags, which, if defined here,
44-
// will be global for your application.
45-
46-
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.datum.yaml)")
47-
4838
// Add subcommands
4939
rootCmd.AddCommand(controller.NewControllerManagerCommand())
5040
}
5141

52-
// initConfig reads in config file and ENV variables if set.
53-
func initConfig() {
54-
if cfgFile != "" {
55-
// Use config file from the flag.
56-
viper.SetConfigFile(cfgFile)
57-
} else {
58-
// Find home directory.
59-
home, err := os.UserHomeDir()
60-
cobra.CheckErr(err)
61-
62-
// Search config in home directory with name ".datum" (without extension).
63-
viper.AddConfigPath(home)
64-
viper.SetConfigType("yaml")
65-
viper.SetConfigName(".datum")
66-
}
67-
68-
viper.AutomaticEnv() // read in environment variables that match
69-
70-
// If a config file is found, read it in.
71-
if err := viper.ReadInConfig(); err == nil {
72-
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
73-
}
74-
}
75-
7642
func main() {
7743
Execute()
7844
}

go.mod

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ require (
99
github.com/onsi/gomega v1.36.1
1010
github.com/spf13/cobra v1.8.1
1111
github.com/spf13/viper v1.20.1
12-
k8s.io/apimachinery v0.32.1
12+
go.miloapis.com/milo v0.0.0-20250624192330-fd15c9091be7
13+
k8s.io/apimachinery v0.33.2
1314
k8s.io/client-go v0.32.1
1415
sigs.k8s.io/controller-runtime v0.20.4
1516
)
1617

1718
require (
18-
cel.dev/expr v0.18.0 // indirect
19+
cel.dev/expr v0.19.1 // indirect
1920
github.com/antlr4-go/antlr/v4 v4.13.0 // indirect
2021
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a // indirect
2122
github.com/beorn7/perks v1.0.1 // indirect
@@ -27,8 +28,8 @@ require (
2728
github.com/evanphx/json-patch/v5 v5.9.11 // indirect
2829
github.com/felixge/httpsnoop v1.0.4 // indirect
2930
github.com/fsnotify/fsnotify v1.8.0 // indirect
30-
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
31-
github.com/go-logr/logr v1.4.2 // indirect
31+
github.com/fxamacker/cbor/v2 v2.8.0 // indirect
32+
github.com/go-logr/logr v1.4.3 // indirect
3233
github.com/go-logr/stdr v1.2.2 // indirect
3334
github.com/go-logr/zapr v1.3.0 // indirect
3435
github.com/go-openapi/jsonpointer v0.21.0 // indirect
@@ -40,12 +41,11 @@ require (
4041
github.com/golang/protobuf v1.5.4 // indirect
4142
github.com/google/btree v1.1.3 // indirect
4243
github.com/google/cel-go v0.22.0 // indirect
43-
github.com/google/gnostic-models v0.6.8 // indirect
44-
github.com/google/go-cmp v0.6.0 // indirect
45-
github.com/google/gofuzz v1.2.0 // indirect
44+
github.com/google/gnostic-models v0.6.9 // indirect
45+
github.com/google/go-cmp v0.7.0 // indirect
4646
github.com/google/pprof v0.0.0-20241029153458-d1b30febd7db // indirect
4747
github.com/google/uuid v1.6.0 // indirect
48-
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 // indirect
48+
github.com/grpc-ecosystem/grpc-gateway/v2 v2.23.0 // indirect
4949
github.com/inconshreveable/mousetrap v1.1.0 // indirect
5050
github.com/josharian/intern v1.0.0 // indirect
5151
github.com/json-iterator/go v1.1.12 // indirect
@@ -67,30 +67,31 @@ require (
6767
github.com/stoewer/go-strcase v1.3.0 // indirect
6868
github.com/subosito/gotenv v1.6.0 // indirect
6969
github.com/x448/float16 v0.8.4 // indirect
70-
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.54.0 // indirect
71-
go.opentelemetry.io/otel v1.29.0 // indirect
72-
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.28.0 // indirect
73-
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.27.0 // indirect
74-
go.opentelemetry.io/otel/metric v1.29.0 // indirect
75-
go.opentelemetry.io/otel/sdk v1.29.0 // indirect
76-
go.opentelemetry.io/otel/trace v1.29.0 // indirect
70+
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
71+
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.55.0 // indirect
72+
go.opentelemetry.io/otel v1.35.0 // indirect
73+
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.32.0 // indirect
74+
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.32.0 // indirect
75+
go.opentelemetry.io/otel/metric v1.35.0 // indirect
76+
go.opentelemetry.io/otel/sdk v1.34.0 // indirect
77+
go.opentelemetry.io/otel/trace v1.35.0 // indirect
7778
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
7879
go.uber.org/multierr v1.11.0 // indirect
7980
go.uber.org/zap v1.27.0 // indirect
8081
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect
81-
golang.org/x/net v0.33.0 // indirect
82+
golang.org/x/net v0.41.0 // indirect
8283
golang.org/x/oauth2 v0.25.0 // indirect
83-
golang.org/x/sync v0.10.0 // indirect
84-
golang.org/x/sys v0.29.0 // indirect
85-
golang.org/x/term v0.27.0 // indirect
86-
golang.org/x/text v0.21.0 // indirect
87-
golang.org/x/time v0.8.0 // indirect
88-
golang.org/x/tools v0.26.0 // indirect
84+
golang.org/x/sync v0.15.0 // indirect
85+
golang.org/x/sys v0.33.0 // indirect
86+
golang.org/x/term v0.32.0 // indirect
87+
golang.org/x/text v0.26.0 // indirect
88+
golang.org/x/time v0.9.0 // indirect
89+
golang.org/x/tools v0.33.0 // indirect
8990
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
90-
google.golang.org/genproto/googleapis/api v0.0.0-20241209162323-e6fa225c2576 // indirect
91-
google.golang.org/genproto/googleapis/rpc v0.0.0-20241223144023-3abc09e42ca8 // indirect
92-
google.golang.org/grpc v1.67.3 // indirect
93-
google.golang.org/protobuf v1.36.1 // indirect
91+
google.golang.org/genproto/googleapis/api v0.0.0-20250106144421-5f5ef82da422 // indirect
92+
google.golang.org/genproto/googleapis/rpc v0.0.0-20250218202821-56aae31c358a // indirect
93+
google.golang.org/grpc v1.71.0 // indirect
94+
google.golang.org/protobuf v1.36.5 // indirect
9495
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
9596
gopkg.in/inf.v0 v0.9.1 // indirect
9697
gopkg.in/yaml.v3 v3.0.1 // indirect
@@ -99,10 +100,11 @@ require (
99100
k8s.io/apiserver v0.32.1 // indirect
100101
k8s.io/component-base v0.32.1 // indirect
101102
k8s.io/klog/v2 v2.130.1 // indirect
102-
k8s.io/kube-openapi v0.0.0-20241105132330-32ad38e42d3f // indirect
103-
k8s.io/utils v0.0.0-20241104100929-3ea5e8cea738 // indirect
103+
k8s.io/kube-openapi v0.0.0-20250318190949-c8a335a9a2ff // indirect
104+
k8s.io/utils v0.0.0-20250604170112-4c0f3b243397 // indirect
104105
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.31.0 // indirect
105-
sigs.k8s.io/json v0.0.0-20241010143419-9aa6b5e7a4b3 // indirect
106-
sigs.k8s.io/structured-merge-diff/v4 v4.4.2 // indirect
106+
sigs.k8s.io/json v0.0.0-20241014173422-cfa47c3a1cc8 // indirect
107+
sigs.k8s.io/randfill v1.0.0 // indirect
108+
sigs.k8s.io/structured-merge-diff/v4 v4.7.0 // indirect
107109
sigs.k8s.io/yaml v1.4.0 // indirect
108110
)

0 commit comments

Comments
 (0)