Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
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
*~
27 changes: 27 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# 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

# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
FROM gcr.io/distroless/static:nonroot
WORKDIR /
COPY --from=builder /workspace/manager .
USER nonroot:nonroot

ENTRYPOINT ["/manager"]
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/controller-gen@v0.2.4 ;\
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: k8s.io
repo: sigs.k8s.io/execution-hook
resources:
- group: apps
kind: ExecutionHook
version: v1alpha1
version: "2"
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
This repo contains `ExecutionHook` CRDs for dynamically executing user’s commands in pods/containers
and an `ExecutionHookController` to manage the hook's lifecycle.

## Usage

Exection Hook uses the [Kubebuilder framework](https://github.com/kubernetes-sigs/kubebuilder).
See the [Kubebuilder documentation](https://book.kubebuilder.io/introduction.html) for building, running and testing the project.

## Community, discussion, contribution, and support

Learn how to engage with the Kubernetes community on the [community page](http://kubernetes.io/community/).
Expand Down
176 changes: 176 additions & 0 deletions api/v1alpha1/executionhook_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
/*

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"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

// ExecutionHookSpec defines the desired state of ExecutionHook
// HookActionName is copied to ExecutionHookSpec by the controller such as
// the Snapshot Controller.
type ExecutionHookSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file

// PodSelection defines how to select pods and containers to run
// the executionhook. If multiple pod/containers are selected, the action will executed on them
// asynchronously. If execution ordering is required, caller has to implement the logic and create
// different hooks in order.
// This field is required.
PodSelection PodSelection `json:"podSelection" protobuf:"bytes,1,opt,name=podSelection"`

// Name of the HookAction. This is required.
ActionName string `json:"actionName" protobuf:"bytes,2,opt,name=actionName"`

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this an ObjectReference to the the HookAction?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is not an ObjectReference because we want to fix the namespace and kind here, but @xing-yang might have a better idea.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. The application snapshot controller is supposed to create this ExecutionHook object dynamically and copy HookActionName to this spec.

}

// PodSelection contains two fields, PodContainerNamesList and PodContainerSelector,
// where one of them must be defined so that the hook controller knows where to
// run the hook.
type PodSelection struct {
// PodContainerNamesList lists the pods/containers on which the ExecutionHook
// should be executed. If not specified, the ExecutionHook controller will find
// all pods and containers based on PodContainerSelector.
// If both PodContainerNamesList and PodContainerSelector are not
// specified, the ExecutionHook cannot be executed and it will fail.
// +optional
PodContainerNamesList []PodContainerNames `json:"podContainerNamesList,omitempty" protobuf:"bytes,1,rep,name=podContainerNamesList"`

// PodContainerSelector is for hook controller to find pods and containers
// based on the pod label selector and container names
// If PodContainerNamesList is specified, this field will not be used.
// +optional
PodContainerSelector *PodContainerSelector `json:"podContainerSelector,omitempty" protobuf:"bytes,2,opt,name=podContainerSelector"`
}

// PodContainerNames lists the containers the ExecutionHook should be executed
// on in a Pod.
type PodContainerNames struct {
// This field is required
PodName string `json:"podName" protobuf:"bytes,1,opt,name=podName"`

// +optional
ContainerNames []string `json:"containerNames,omitempty" protobuf:"bytes,2,rep,name=containerNames"`
}

// PodContainerSelector defines the selector and containers the ExecutionHook
// should be executed on.
type PodContainerSelector struct {
// PodSelector specifies a label query over a set of pods.
// +optional
PodSelector *metav1.LabelSelector `json:"podSelector,omitempty" protobuf:"bytes,1,opt,name=podSelector"`

// If specified, controller only select the containers that are listed from the selected pods based on PodSelector.
// Otherwise, all containers of the pods will be selected
// +optional
ContainerList []string `json:"containerList,omitempty" protobuf:"bytes,2,rep,name=containerList"`
}

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

// This is a list of ContainerExecutionHookStatus, with each status representing
// information about how hook is executed in a container, including pod name,
// container name, ActionTimestamp, ActionSucceed, etc.
// +optional
HookStatuses []ContainerExecutionHookStatus `json:"containerExecutionHookStatuses,omitempty" protobuf:"bytes,1,rep,name=containerExecutionHookStatuses"`
}

// ContainerExecutionHookStatus represents the current state of a hook for a specific
// container in a pod
type ContainerExecutionHookStatus struct {
// This field is required
PodName string `json:"podName" protobuf:"bytes,1,opt,name=podName"`

// This field is required
ContainerName string `json:"containerName" protobuf:"bytes,2,opt,name=containerName"`

// If not set, it is nil, indicating Action has not started
// If set, it means Action has started at the specified time
// +optional
Timestamp *metav1.Time `json:"actionTimestamp,omitempty" protobuf:"bytes,3,opt,name=actionTimestamp"`

// Succeed is set to true when the action is executed in the container successfully.
// It will be set to false if the action cannot be executed successfully after
// ActionTimeoutSeconds passes.
// +optional
Succeed *bool `json:"actionSucceed,omitempty" protobuf:"varint,4,opt,name=actionSucceed"`

// The last error encountered when executing the action. The hook controller might
// update this field each time it retries the execution.
// +optional
Error *HookError `json:"error,omitempty" protobuf:"bytes,5,opt,name=error"`
}

// HookError describes the error occurred from hook execution.
type HookError struct {
// Type of the error
// This is required
ErrorType ErrorType `json:"errorType" protobuf:"bytes,1,opt,name=errorType"`

// Error message
// +optional
Message *string `json:"message,omitempty" protobuf:"bytes,2,opt,name=message"`

// More detailed reason why error happens
// +optional
Reason *string `json:"reason,omitempty" protobuf:"bytes,3,opt,name=reason"`

// It indicates when the error occurred
// +optional
Timestamp *metav1.Time `json:"timestamp,omitempty" protobuf:"bytes,4,opt,name=timestamp"`
}

// ErrorType defines the type of error occurred from hook execution.
type ErrorType string

// More error types could be added, e.g., Forbidden, Unauthorized, AlreadyInProgress, etc.
const (
// The execution hook times out
Timeout ErrorType = "Timeout"

// The execution hook fails with an error
Error ErrorType = "Error"
)

// +kubebuilder:object:root=true

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

Spec ExecutionHookSpec `json:"spec,omitempty"`
Status ExecutionHookStatus `json:"status,omitempty"`
}

// +kubebuilder:object:root=true

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

func init() {
SchemeBuilder.Register(&ExecutionHook{}, &ExecutionHookList{})
}
35 changes: 35 additions & 0 deletions api/v1alpha1/groupversion_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*

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 apps v1alpha1 API group
// +kubebuilder:object:generate=true
// +groupName=apps.k8s.io
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: "apps.k8s.io", 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