Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge POC into consul-k8s #309

Merged
merged 20 commits into from
Aug 27, 2020
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 53 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export GIT_DESCRIBE
export GOLDFLAGS
export GOTAGS


CRD_OPTIONS ?= "crd:trivialVersions=true"

################
# CI Variables #
Expand Down Expand Up @@ -79,7 +79,7 @@ else
DEV_PUSH_ARG=--no-push
endif

all: bin
all: bin ctrl-generate

bin:
@$(SHELL) $(CURDIR)/build-support/scripts/build-local.sh
Expand Down Expand Up @@ -128,6 +128,57 @@ clean:
$(CURDIR)/bin \
$(CURDIR)/pkg

# Run controller tests
ENVTEST_ASSETS_DIR=$(shell pwd)/testbin
ctrl-test: ctrl-generate ctrl-manifests
mkdir -p ${ENVTEST_ASSETS_DIR}
test -f ${ENVTEST_ASSETS_DIR}/setup-envtest.sh || curl -sSLo ${ENVTEST_ASSETS_DIR}/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/master/hack/setup-envtest.sh
source ${ENVTEST_ASSETS_DIR}/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test ./...

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

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

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

# 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/controller-gen@v0.3.0 ;\
rm -rf $$CONTROLLER_GEN_TMP_DIR ;\
}
CONTROLLER_GEN=$(GOBIN)/controller-gen
else
CONTROLLER_GEN=$(shell which controller-gen)
endif

kustomize:
ifeq (, $(shell which kustomize))
@{ \
set -e ;\
KUSTOMIZE_GEN_TMP_DIR=$$(mktemp -d) ;\
cd $$KUSTOMIZE_GEN_TMP_DIR ;\
go mod init tmp ;\
go get sigs.k8s.io/kustomize/kustomize/v3@v3.5.4 ;\
rm -rf $$KUSTOMIZE_GEN_TMP_DIR ;\
}
KUSTOMIZE=$(GOBIN)/kustomize
else
KUSTOMIZE=$(shell which kustomize)
endif

# In CircleCI, the linux binary will be attached from a previous step at pkg/bin/linux_amd64/. This make target
# should only run in CI and not locally.
Expand Down
6 changes: 6 additions & 0 deletions PROJECT
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
domain: hashicorp.com
thisisnotashwin marked this conversation as resolved.
Show resolved Hide resolved
layout: go.kubebuilder.io/v2
repo: github.com/hashicorp/consul-k8s
version: 3-alpha
thisisnotashwin marked this conversation as resolved.
Show resolved Hide resolved
plugins:
go.operator-sdk.io/v2-alpha: {}
20 changes: 20 additions & 0 deletions api/v1alpha1/groupversion_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Package v1alpha1 contains API Schema definitions for the consul.hashicorp.com v1alpha1 API group
// +kubebuilder:object:generate=true
// +groupName=consul.hashicorp.com
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: "consul.hashicorp.com", 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
)
105 changes: 105 additions & 0 deletions api/v1alpha1/servicedefaults_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package v1alpha1

import (
consulapi "github.com/hashicorp/consul/api"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
thisisnotashwin marked this conversation as resolved.
Show resolved Hide resolved
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

// ServiceDefaultsSpec defines the desired state of ServiceDefaults
type ServiceDefaultsSpec struct {
Protocol string `json:"protocol,omitempty"`
MeshGateway MeshGatewayConfig `json:"meshGateway,omitempty"`
Expose ExposeConfig `json:"expose,omitempty"`
ExternalSNI string `json:"externalSNI,omitempty"`
}

// ServiceDefaultsStatus defines the observed state of ServiceDefaults
type ServiceDefaultsStatus struct {
Status `json:",inline"`
}

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status

// ServiceDefaults is the Schema for the servicedefaults API
// +kubebuilder:printcolumn:name="Synced",type="string",JSONPath=".status.conditions[?(@.type==\"Synced\")].status",description="The sync status of the resource with Consul"
type ServiceDefaults struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec ServiceDefaultsSpec `json:"spec,omitempty"`
Status ServiceDefaultsStatus `json:"status,omitempty"`
}

// +kubebuilder:object:root=true

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

func init() {
SchemeBuilder.Register(&ServiceDefaults{}, &ServiceDefaultsList{})
}

func (s *ServiceDefaults) ToConsul() *consulapi.ServiceConfigEntry {
thisisnotashwin marked this conversation as resolved.
Show resolved Hide resolved
return &consulapi.ServiceConfigEntry{
thisisnotashwin marked this conversation as resolved.
Show resolved Hide resolved
Kind: consulapi.ServiceDefaults,
Name: s.Name,
//Namespace: s.Namespace, // todo: don't set this unless enterprise
Protocol: s.Spec.Protocol,
MeshGateway: consulapi.MeshGatewayConfig{
Mode: s.gatewayMode(),
},
Expose: consulapi.ExposeConfig{
Checks: s.Spec.Expose.Checks,
Paths: s.parseExposePath(),
},
ExternalSNI: s.Spec.ExternalSNI,
}
}

func (s *ServiceDefaults) parseExposePath() []consulapi.ExposePath {
var paths []consulapi.ExposePath
for _, path := range s.Spec.Expose.Paths {
paths = append(paths, consulapi.ExposePath{
ListenerPort: path.ListenerPort,
Path: path.Path,
LocalPathPort: path.LocalPathPort,
Protocol: path.Protocol,
ParsedFromCheck: path.ParsedFromCheck,
})
}
return paths
}

func (s *ServiceDefaults) gatewayMode() consulapi.MeshGatewayMode {
switch s.Spec.MeshGateway.Mode {
case "local":
thisisnotashwin marked this conversation as resolved.
Show resolved Hide resolved
return consulapi.MeshGatewayModeLocal
case "none":
return consulapi.MeshGatewayModeNone
case "remote":
return consulapi.MeshGatewayModeRemote
default:
return consulapi.MeshGatewayModeDefault
}
}

// this will check if the consul struct shares the same spec as the spec of the resource
thisisnotashwin marked this conversation as resolved.
Show resolved Hide resolved
func (s *ServiceDefaults) MatchesConsul(entry *consulapi.ServiceConfigEntry) bool {
matches := s.Name == entry.GetName() &&
s.Spec.Protocol == entry.Protocol &&
s.Spec.MeshGateway.Mode == string(entry.MeshGateway.Mode) &&
s.Spec.Expose.Matches(entry.Expose) &&
s.Spec.ExternalSNI == entry.ExternalSNI
if !matches {
return false
}
return true
thisisnotashwin marked this conversation as resolved.
Show resolved Hide resolved
}
Loading