Skip to content

Commit

Permalink
Import dnsmasq-controller
Browse files Browse the repository at this point in the history
  • Loading branch information
kvaps committed Apr 14, 2020
0 parents commit c745c31
Show file tree
Hide file tree
Showing 48 changed files with 2,054 additions and 0 deletions.
24 changes: 24 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
bin

# Test binary, build with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Kubernetes Generated files - skip generated files, except for vendored files

!vendor/**/zz_generated.*

# editor and IDE paraphernalia
.idea
*.swp
*.swo
*~
24 changes: 24 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Build the manager binary
FROM golang:1.13 as builder

WORKDIR /workspace
# Copy the Go Modules manifests
COPY go.mod go.mod
COPY go.sum go.sum
# cache deps before building and copying source so that we don't need to re-download as much
# and so that source changes don't invalidate our downloaded layer
RUN go mod download

# Copy the go source
COPY main.go main.go
COPY api/ api/
COPY controllers/ controllers/

# Build
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o manager main.go

FROM alpine:3.11
RUN apk add --no-cache dnsmasq
COPY --from=builder /workspace/manager /dnsmasq-controller

ENTRYPOINT ["/dnsmasq-controller"]
80 changes: 80 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@

# Image URL to use all building/pushing image targets
IMG ?= controller:latest
# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
CRD_OPTIONS ?= "crd:trivialVersions=true"

# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
GOBIN=$(shell go env GOPATH)/bin
else
GOBIN=$(shell go env GOBIN)
endif

all: manager

# Run tests
test: generate fmt vet manifests
go test ./... -coverprofile cover.out

# Build manager binary
manager: generate fmt vet
go build -o bin/manager main.go

# Run against the configured Kubernetes cluster in ~/.kube/config
run: generate fmt vet manifests
go run ./main.go

# Install CRDs into a cluster
install: manifests
kustomize build config/crd | kubectl apply -f -

# Uninstall CRDs from a cluster
uninstall: manifests
kustomize build config/crd | kubectl delete -f -

# Deploy controller in the configured Kubernetes cluster in ~/.kube/config
deploy: manifests
cd config/manager && kustomize edit set image controller=${IMG}
kustomize build config/default | kubectl apply -f -

# Generate manifests e.g. CRD, RBAC etc.
manifests: controller-gen
$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases

# Run go fmt against code
fmt:
go fmt ./...

# Run go vet against code
vet:
go vet ./...

# Generate code
generate: controller-gen
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."

# Build the docker image
docker-build: test
docker build . -t ${IMG}

# Push the docker image
docker-push:
docker push ${IMG}

# find or download controller-gen
# download controller-gen if necessary
controller-gen:
ifeq (, $(shell which controller-gen))
@{ \
set -e ;\
CONTROLLER_GEN_TMP_DIR=$$(mktemp -d) ;\
cd $$CONTROLLER_GEN_TMP_DIR ;\
go mod init tmp ;\
go get sigs.k8s.io/controller-tools/cmd/[email protected] ;\
rm -rf $$CONTROLLER_GEN_TMP_DIR ;\
}
CONTROLLER_GEN=$(GOBIN)/controller-gen
else
CONTROLLER_GEN=$(shell which controller-gen)
endif
7 changes: 7 additions & 0 deletions PROJECT
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
domain: kvaps.cf
repo: github.com/kvaps/dnsmasq-controller
resources:
- group: dnsmasq
kind: DnsmasqOptionSet
version: v1alpha1
version: "2"
64 changes: 64 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Dnsmasq-controller

A Dnsmasq-controller for Kubernetes, implemented in go using [kubebuilder](https://kubebuilder.io/).

## Status

![GitHub](https://img.shields.io/badge/status-alpha-blue?style=for-the-badge)
![GitHub](https://img.shields.io/github/license/kristofferahl/healthchecksio-operator?style=for-the-badge)
![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/kristofferahl/healthchecksio-operator?style=for-the-badge)

## Supported resources
- DnsmasqOptionSet

## Example
```yaml
---
apiVersion: dnsmasq.kvaps.cf/v1alpha1
kind: DnsmasqOptionSet
metadata:
name: node1
spec:
controller: ""
options:
- key: host-record
value: node1,10.9.8.7
- key: dhcp-host
value: set:ltsp1,a0:1d:48:b5:ae:a8,a0:1d:48:b5:ae:a9,id:*,10.9.8.7,node1,infinite
```
### Configuration
| Flag | Type | Required | Description |
|---------------------------|--------|----------|-----------------------------------------------------------------------------------------------------------------------------------------|
| `-cleanup` | bool | false | Cleanup Dnsmasq config directory before start. |
| `-conf-dir` | string | false | Dnsmasq config directory for write configuration to. (default "/etc/dnsmasq.d") |
| `-controller` | string | false | Name of the controller this controller satisfies. (default "") |
| `-development` | bool | false | Run the controller in development mode. |
| `-enable-leader-election` | bool | false | Enable leader election for controller manager. Enabling this will ensure there is only one active controller manager. |
| `-kubeconfig` | string | false | Paths to a kubeconfig. Only required if out-of-cluster. |
| `-log-level` | string | false | The log level used by the operator. (default "info") |
| `-metrics-addr` | string | false | The address the metric endpoint binds to. (default ":8080") |
| `-sync-delay` | int | false | Time in seconds to syncronise Dnsmasq configuration. (default 1) |
| `-watch-namespace` | string | false | Namespace the controller watches for updates to Kubernetes objects. All namespaces are watched if this parameter is left empty. |
| `--` | array | false | Additional command line arguments for Dnsmasq may be specified after `--` (read [dnsmasq-man] for more details) |

[dnsmasq-man]: http://www.thekelleys.org.uk/dnsmasq/docs/dnsmasq-man.html

## Development

### Pre-requisites
- [Go](https://golang.org/) 1.13 or later
- [Kubebuilder](https://kubebuilder.io/) 2.3.1
- [Kubernetes](https://kubernetes.io/) cluster

### Getting started
```bash
make install
make run
```

### Running tests
```bash
make test
```
65 changes: 65 additions & 0 deletions api/v1alpha1/dnsmasqoptionset_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// DnsmasqOptionSetSpecOption defines the desired state of DnsmasqOptionSetSpec
type DnsmasqOptionSetSpecOption struct {
// +kubebuilder:validation:Enum=dhcp-range;dhcp-host;dhcp-userclass;dhcp-circuitid;dhcp-remoteid;dhcp-subscrid;dhcp-ignore;dhcp-broadcast;mx-host;dhcp-boot;dhcp-option;dhcp-option-force;server;rev-server;local;domain;dhcp-vendorclass;alias;dhcp-vendorclass;srv-host;txt-record;ptr-record;bootp-dynamic;dhcp-mac;dhcp-ignore-names;rebind-domain-ok;dhcp-match;dhcp-name-match;naptr-record;dhcp-generate-names;cname;pxe-service;add-mac;dhcp-duid;host-record;caa-record;dns-rr;auth-zone;synth-domain
Key string `json:"key,omitempty"`
Value string `json:"value,omitempty"`
}

// DnsmasqOptionSetSpec defines the desired state of DnsmasqOptionSet
type DnsmasqOptionSetSpec struct {
Controller string `json:"controller,omitempty"`
Options []DnsmasqOptionSetSpecOption `json:"options,omitempty"`
}

// DnsmasqOptionSetStatus defines the observed state of DnsmasqOptionSet
type DnsmasqOptionSetStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file
}

// +kubebuilder:resource:shortName=dnsmasqopt;dnsmasqopts
// +kubebuilder:object:root=true

// DnsmasqOptionSet is the Schema for the dnsmasqoptionsets API
type DnsmasqOptionSet struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec DnsmasqOptionSetSpec `json:"spec,omitempty"`
Status DnsmasqOptionSetStatus `json:"status,omitempty"`
}

// +kubebuilder:object:root=true

// DnsmasqOptionSetList contains a list of DnsmasqOptionSet
type DnsmasqOptionSetList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []DnsmasqOptionSet `json:"items"`
}

func init() {
SchemeBuilder.Register(&DnsmasqOptionSet{}, &DnsmasqOptionSetList{})
}
36 changes: 36 additions & 0 deletions api/v1alpha1/groupversion_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Package v1alpha1 contains API Schema definitions for the dnsmasq v1alpha1 API group
// +kubebuilder:object:generate=true
// +groupName=dnsmasq.kvaps.cf
package v1alpha1

import (
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/scheme"
)

var (
// GroupVersion is group version used to register these objects
GroupVersion = schema.GroupVersion{Group: "dnsmasq.kvaps.cf", Version: "v1alpha1"}

// SchemeBuilder is used to add go types to the GroupVersionKind scheme
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion}

// AddToScheme adds the types in this group-version to the given scheme.
AddToScheme = SchemeBuilder.AddToScheme
)
Loading

0 comments on commit c745c31

Please sign in to comment.