Skip to content

Commit

Permalink
Merge pull request #20 from garethjevans/store-as-crd
Browse files Browse the repository at this point in the history
feat: implement storage functionality
  • Loading branch information
garethjevans authored Feb 25, 2021
2 parents a6cabb3 + 73cebb0 commit 4adba1d
Show file tree
Hide file tree
Showing 36 changed files with 1,891 additions and 163 deletions.
8 changes: 4 additions & 4 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---
linters-settings:
depguard:
list-type: blacklist
Expand Down Expand Up @@ -129,10 +130,9 @@ issues:
text: "unnecessaryDefer:"
run:
skip-dirs:
- test/testdata_etc
- internal/cache
- internal/renameio
- internal/robustio
- pkg/client
skip-files:
- pkg/api/captainhookio/v1alpha1/zz_generated.deepcopy.go
# golangci.com configuration
# https://github.com/golangci/golangci/wiki/Configuration
service:
Expand Down
21 changes: 21 additions & 0 deletions charts/captain-hook/templates/crds.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: hooks.captainhook.io
labels:
app: {{ template "captain-hook.fullname" . }}
chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
release: "{{ .Release.Name }}"
heritage: "{{ .Release.Service }}"
spec:
group: captainhook.io
names:
kind: Hook
listKind: HookList
plural: hooks
shortNames:
- hook
singular: hook
scope: Namespaced
version: v1alpha1
7 changes: 6 additions & 1 deletion charts/captain-hook/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ spec:
resources:
{{- toYaml .Values.resources | nindent 12 }}
env:
{{- toYaml .Values.env | nindent 12 }}
- name: HOOK_PATH
value: {{ .Values.hookPath }}
- name: FORWARD_URL
value: {{ .Values.forwardURL }}
- name: INSECURE_RELAY
value: {{ .Values.insecureRelay | quote }}
{{- with .Values.nodeSelector }}
nodeSelector:
{{- toYaml . | nindent 8 }}
Expand Down
19 changes: 12 additions & 7 deletions charts/captain-hook/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ image:
# Overrides the image tag whose default is the chart appVersion.
tag: ""

env:
- name: HOOK_PATH
value: /hook
- name: FORWARD_URL
value: http://jenkins:8080/github-webhook/
# Service Configuration
hookPath: /hook
forwardURL: http://jenkins:8080/github-webhook/
insecureRelay: false

imagePullSecrets: []
nameOverride: ""
Expand Down Expand Up @@ -51,8 +50,14 @@ ingress:
# kubernetes.io/ingress.class: nginx
# kubernetes.io/tls-acme: "true"
hosts:
- host: chart-example.local
paths: []
- paths:
- backend:
service:
name: captain-hook
port:
number: 8080
pathType: ImplementationSpecific

tls: []
# - secretName: chart-example-tls
# hosts:
Expand Down
8 changes: 5 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ module github.com/garethjevans/captain-hook
go 1.15

require (
github.com/cenkalti/backoff v2.2.1+incompatible // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/cenkalti/backoff v2.2.1+incompatible
github.com/gorilla/mux v1.8.0
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.8.0
github.com/spf13/cobra v1.1.3
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.7.0 // indirect
github.com/stretchr/testify v1.7.0
k8s.io/apimachinery v0.20.4
k8s.io/client-go v0.20.4
)
411 changes: 263 additions & 148 deletions go.sum

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions hack/custom-boilerplate.go.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/*
Generated Code
*/
85 changes: 85 additions & 0 deletions hack/generate-groups.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#!/usr/bin/env bash

# Copyright 2017 The Kubernetes Authors.
#
# 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.

set -o errexit
set -o nounset
set -o pipefail

# generate-groups generates everything for a project with external types only, e.g. a project based
# on CustomResourceDefinitions.

if [ "$#" -lt 4 ] || [ "${1}" == "--help" ]; then
cat <<EOF
Usage: $(basename "$0") <generators> <output-package> <apis-package> <groups-versions> ...
<generators> the generators comma separated to run (deepcopy,defaulter,client,lister,informer) or "all".
<output-package> the output package name (e.g. github.com/example/project/pkg/generated).
<apis-package> the external types dir (e.g. github.com/example/api or github.com/example/project/pkg/apis).
<groups-versions> the groups and their versions in the format "groupA:v1,v2 groupB:v1 groupC:v2", relative
to <api-package>.
... arbitrary flags passed to all generator binaries.
Examples:
$(basename "$0") all github.com/example/project/pkg/client github.com/example/project/pkg/apis "foo:v1 bar:v1alpha1,v1beta1"
$(basename "$0") deepcopy,client github.com/example/project/pkg/client github.com/example/project/pkg/apis "foo:v1 bar:v1alpha1,v1beta1"
EOF
exit 0
fi

GENS="$1"
OUTPUT_PKG="$2"
APIS_PKG="$3"
GROUPS_WITH_VERSIONS="$4"
shift 4

function codegen::join() { local IFS="$1"; shift; echo "$*"; }

# enumerate group versions
FQ_APIS=() # e.g. k8s.io/api/apps/v1
for GVs in ${GROUPS_WITH_VERSIONS}; do
IFS=: read -r G Vs <<<"${GVs}"

# enumerate versions
for V in ${Vs//,/ }; do
FQ_APIS+=("${APIS_PKG}/${G}/${V}")
done
done

if [ "${GENS}" = "all" ] || grep -qw "deepcopy" <<<"${GENS}"; then
echo "Generating deepcopy funcs"
"deepcopy-gen" --input-dirs "$(codegen::join , "${FQ_APIS[@]}")" -O zz_generated.deepcopy --bounding-dirs "${APIS_PKG}" "$@"
fi

if [ "${GENS}" = "all" ] || grep -qw "client" <<<"${GENS}"; then
echo "Generating clientset for ${GROUPS_WITH_VERSIONS} at ${OUTPUT_PKG}/${CLIENTSET_PKG_NAME:-clientset}"
"client-gen" --clientset-name "${CLIENTSET_NAME_VERSIONED:-versioned}" --input-base "" --input "$(codegen::join , "${FQ_APIS[@]}")" --output-package "${OUTPUT_PKG}/${CLIENTSET_PKG_NAME:-clientset}" "$@"
fi

if [ "${GENS}" = "all" ] || grep -qw "lister" <<<"${GENS}"; then
echo "Generating listers for ${GROUPS_WITH_VERSIONS} at ${OUTPUT_PKG}/listers"
"lister-gen" --input-dirs "$(codegen::join , "${FQ_APIS[@]}")" --output-package "${OUTPUT_PKG}/listers" "$@"
fi

if [ "${GENS}" = "all" ] || grep -qw "informer" <<<"${GENS}"; then
echo "Generating informers for ${GROUPS_WITH_VERSIONS} at ${OUTPUT_PKG}/informers"
"informer-gen" \
--input-dirs "$(codegen::join , "${FQ_APIS[@]}")" \
--versioned-clientset-package "${OUTPUT_PKG}/${CLIENTSET_PKG_NAME:-clientset}/${CLIENTSET_NAME_VERSIONED:-versioned}" \
--listers-package "${OUTPUT_PKG}/listers" \
--output-package "${OUTPUT_PKG}/informers" \
"$@"
fi
58 changes: 58 additions & 0 deletions hack/update-codegen.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env bash

# Copyright 2017 The Kubernetes Authors.
#
# 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.

set -o errexit
set -o nounset
set -o pipefail

#CODEGEN_PKG=${CODEGEN_PKG:-$(cd "${SCRIPT_ROOT}"; ls -d -1 ./vendor/k8s.io/code-generator 2>/dev/null || echo ./cmd/code-generator)}
GENERATOR_VERSION=v0.20.4
(
# To support running this script from anywhere, we have to first cd into this directory
# so we can install the tools.
cd "$(dirname "${0}")"
go get k8s.io/code-generator/cmd/{defaulter-gen,client-gen,lister-gen,informer-gen,deepcopy-gen}@$GENERATOR_VERSION
)

SCRIPT_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
rm -rf "${SCRIPT_ROOT}"/pkg/client
# generate the code with:
# --output-base because this script should also be able to run inside the vendor dir of
# k8s.io/kubernetes. The output-base is needed for the generators to output into the vendor dir
# instead of the $GOPATH directly. For normal projects this can be dropped.
bash hack/generate-groups.sh all \
github.com/garethjevans/captain-hook/pkg/client github.com/garethjevans/captain-hook/pkg/api \
captainhookio:v1alpha1 \
--output-base "$(dirname "${BASH_SOURCE[0]}")/../../../.." \
--go-header-file "${SCRIPT_ROOT}"/hack/custom-boilerplate.go.txt


#cp -R "${SCRIPT_ROOT}"/v4/pkg/client/ "${SCRIPT_ROOT}"/pkg/client
#cp -R "${SCRIPT_ROOT}"/v4/pkg/apis/jenkins.io/v1/zz_generated.deepcopy.go "${SCRIPT_ROOT}"/pkg/apis/jenkins.io/v1/zz_generated.deepcopy.go

#rm -rf "${SCRIPT_ROOT}"/v4

#bash hack/generate-groups.sh all \
# github.com/jenkins-x/jx-api/v4/pkg/generated/core github.com/jenkins-x/jx-api/v4/pkg/apis \
# core:v4beta1 \
# --output-base "$(dirname "${BASH_SOURCE[0]}")/../../../.." \
# --go-header-file "${SCRIPT_ROOT}"/hack/custom-boilerplate.go.txt
#
#
#cp -R "${SCRIPT_ROOT}"/v4/pkg/generated/ "${SCRIPT_ROOT}"/pkg/generated
#cp -R "${SCRIPT_ROOT}"/v4/pkg/apis/core/v4beta1/zz_generated.deepcopy.go "${SCRIPT_ROOT}"/pkg/apis/core/v4beta1/zz_generated.deepcopy.go
#
#rm -rf "${SCRIPT_ROOT}"/v4
15 changes: 15 additions & 0 deletions pkg/api/captainhookio/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package captainhookio

const (
// GroupName is the Captain Hook API group name
GroupName = "captainhook.io"

// Version is the Captain Hook API group version
Version = "v1alpha1"

// GroupAndVersion is the Captain Hook API Group and version
GroupAndVersion = GroupName + "/" + Version

// Package is the Go package in which the apis live
Package = "github.com/garethjevans/captain-hook/pkg/api"
)
4 changes: 4 additions & 0 deletions pkg/api/captainhookio/v1alpha1/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// +k8s:deepcopy-gen=package
// +k8s:openapi-gen=true
// +groupName=captainhook.io
package v1alpha1
60 changes: 60 additions & 0 deletions pkg/api/captainhookio/v1alpha1/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package v1alpha1

import (
"github.com/garethjevans/captain-hook/pkg/api/captainhookio"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
)

// SchemeGroupVersion is group version used to register these objects.
var SchemeGroupVersion = schema.GroupVersion{Group: captainhookio.GroupName, Version: captainhookio.Version}

// Kind takes an unqualified kind and returns back a Group qualified GroupKind.
func Kind(kind string) schema.GroupKind {
return SchemeGroupVersion.WithKind(kind).GroupKind()
}

// Resource takes an unqualified resource and returns a Group qualified GroupResource.
func Resource(resource string) schema.GroupResource {
return SchemeGroupVersion.WithResource(resource).GroupResource()
}

var (
// SchemeBuilder for building the schema :).
SchemeBuilder = runtime.NewSchemeBuilder(addKnownTypes)
// AddToScheme helper
AddToScheme = SchemeBuilder.AddToScheme
)

func init() {
// We only register manually written functions here. The registration of the
// generated functions takes place in the generated files. The separation
// makes the code compile even when the generated files are missing.
SchemeBuilder.Register(addKnownTypes)
}

// Adds the list of known types to Scheme.
func addKnownTypes(scheme *runtime.Scheme) error {
scheme.AddKnownTypes(SchemeGroupVersion,
&Hook{},
&HookList{},
)
metav1.AddToGroupVersion(scheme, SchemeGroupVersion)
return nil
}

type ResourceReference struct {
// API version of the referent.
APIVersion string `json:"apiVersion,omitempty" protobuf:"bytes,5,opt,name=apiVersion"`
// Kind of the referent.
// More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds
Kind string `json:"kind" protobuf:"bytes,1,opt,name=kind"`
// Name of the referent.
// More info: http://kubernetes.io/docs/user-guide/identifiers#names
Name string `json:"name" protobuf:"bytes,3,opt,name=name"`
// UID of the referent.
// More info: http://kubernetes.io/docs/user-guide/identifiers#uids
UID types.UID `json:"uid,omitempty" protobuf:"bytes,4,opt,name=uid,casttype=k8s.io/apimachinery/pkg/types.UID"`
}
60 changes: 60 additions & 0 deletions pkg/api/captainhookio/v1alpha1/types_hook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package v1alpha1

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

// +genclient
// +genclient:noStatus
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +k8s:openapi-gen=true

// Hook represents a webhook.
type Hook struct {
metav1.TypeMeta `json:",inline"`
// Standard object's metadata.
// More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata
// +optional
metav1.ObjectMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`

Spec HookSpec `json:"spec,omitempty" protobuf:"bytes,2,opt,name=spec"`
Status HookStatus `json:"status,omitempty" protobuf:"bytes,3,opt,name=status"`
}

// HookSpec is the specification of a Hook.
type HookSpec struct {
ForwardURL string `json:"forwardURL" protobuf:"bytes,1,opt,name=forwardURL"`
Body string `json:"body" protobuf:"bytes,2,opt,name=body"`
Headers map[string][]string `json:"headers,omitempty" protobuf:"bytes,3,opt,name=headers"`
}

// HookStatus is the status for a Hook resource.
type HookStatus struct {
Status HookStatusType `json:"status,omitempty" protobuf:"bytes,1,opt,name=status"`
Attempts int `json:"attempts,omitempty" protobuf:"bytes,2,opt,name=attempts"`
Message string `json:"message,omitempty" protobuf:"bytes,3,opt,name=message"`
}

// HookStatusType is the status of a hook; usually success or failed at completion.
type HookStatusType string

const (
// HookStatusTypeNone an hook step has not started yet.
HookStatusTypeNone HookStatusType = ""
// HookStatusTypePending the hook currently being relayed.
HookStatusTypePending HookStatusType = "Pending"
// HookStatusTypeStatus the hook has been relayed.
HookStatusTypeSuccess HookStatusType = "Success"
// ReleaseStatusTypeFailed the hook has failed to be relayed.
HookStatusTypeFailed HookStatusType = "Failed"
)

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

// HookList is a list of TypeMeta resources.
type HookList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata"`

Items []Hook `json:"items"`
}
Loading

0 comments on commit 4adba1d

Please sign in to comment.