Skip to content

Commit

Permalink
rename operator to controller
Browse files Browse the repository at this point in the history
  • Loading branch information
shreddedbacon committed Aug 28, 2020
1 parent 11e4237 commit e96fe17
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 73 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ else
CONTROLLER_GEN=$(shell which controller-gen)
endif

operator-test:
./operator-test.sh
controller-test:
./controller-test.sh

clean:
docker-compose down
Expand Down
2 changes: 1 addition & 1 deletion config/default/envs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ spec:
- name: RABBITMQ_HOSTNAME
value: '172.17.0.1:5672'
- name: LAGOON_TARGET_NAME
value: 'ci-local-operator-k8s'
value: 'ci-local-controller-kubernetes'
69 changes: 30 additions & 39 deletions operator-test.sh → controller-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@
KIND_VER=v1.17.5
# or get the latest tagged version of a specific k8s version of kind
#KIND_VER=$(curl -s https://hub.docker.com/v2/repositories/kindest/node/tags | jq -r '.results | .[].name' | grep 'v1.17' | sort -Vr | head -1)
KIND_NAME=kbd-operator-test
OPERATOR_IMAGE=amazeeio/lagoon-builddeploy:test-tag
KIND_NAME=kbd-controller-test
CONTROLLER_IMAGE=amazeeio/lagoon-builddeploy:test-tag


BUILD_OPERATOR=true
OPERATOR_NAMESPACE=lagoon-builddeploy
if [ ! -z "$BUILD_OPERATOR" ]; then
OPERATOR_NAMESPACE=lagoon-kbd-system
BUILD_CONTROLLER=true
CONTROLLER_NAMESPACE=lagoon-builddeploy
if [ ! -z "$BUILD_CONTROLLER" ]; then
CONTROLLER_NAMESPACE=lagoon-kbd-system
fi
CHECK_TIMEOUT=10

NS=drupal-example-install
LBUILD=lagoon-build-7m5zypx

check_operator_log () {
echo "=========== OPERATOR LOG ============"
kubectl logs $(kubectl get pods -n ${OPERATOR_NAMESPACE} --no-headers | awk '{print $1}') -c manager -n ${OPERATOR_NAMESPACE}
if $(kubectl logs $(kubectl get pods -n ${OPERATOR_NAMESPACE} --no-headers | awk '{print $1}') -c manager -n ${OPERATOR_NAMESPACE} | grep -q "Build ${LBUILD} Failed")
check_controller_log () {
echo "=========== CONTROLLER LOG ============"
kubectl logs $(kubectl get pods -n ${CONTROLLER_NAMESPACE} --no-headers | awk '{print $1}') -c manager -n ${CONTROLLER_NAMESPACE}
if $(kubectl logs $(kubectl get pods -n ${CONTROLLER_NAMESPACE} --no-headers | awk '{print $1}') -c manager -n ${CONTROLLER_NAMESPACE} | grep -q "Build ${LBUILD} Failed")
then
# build failed, exit 1
tear_down
Expand Down Expand Up @@ -95,30 +95,30 @@ volumeBindingMode: WaitForFirstConsumer
EOF
}

build_deploy_operator () {
echo "==> Build and deploy operator"
build_deploy_controller () {
echo "==> Build and deploy controller"
make test
make docker-build IMG=${OPERATOR_IMAGE}
kind load docker-image ${OPERATOR_IMAGE} --name ${KIND_NAME}
make deploy IMG=${OPERATOR_IMAGE}
make docker-build IMG=${CONTROLLER_IMAGE}
kind load docker-image ${CONTROLLER_IMAGE} --name ${KIND_NAME}
make deploy IMG=${CONTROLLER_IMAGE}

CHECK_COUNTER=1
echo "==> Ensure operator is running"
until $(kubectl get pods -n ${OPERATOR_NAMESPACE} --no-headers | grep -q "Running")
echo "==> Ensure controller is running"
until $(kubectl get pods -n ${CONTROLLER_NAMESPACE} --no-headers | grep -q "Running")
do
if [ $CHECK_COUNTER -lt $CHECK_TIMEOUT ]; then
let CHECK_COUNTER=CHECK_COUNTER+1
echo "Operator not running yet"
echo "Controller not running yet"
sleep 5
else
echo "Timeout of $CHECK_TIMEOUT for operator startup reached"
check_operator_log
echo "Timeout of $CHECK_TIMEOUT for controller startup reached"
check_controller_log
tear_down
echo "================ END ================"
exit 1
fi
done
echo "==> Operator is running"
echo "==> Controller is running"
}


Expand All @@ -129,44 +129,35 @@ check_lagoon_build () {
until $(kubectl get pods -n ${NS} --no-headers | grep -iq "Running")
do
if [ $CHECK_COUNTER -lt $CHECK_TIMEOUT ]; then
# if $(kubectl -n ${NS} get lagoonbuilds/${LBUILD} -o yaml | grep -q "lagoon.sh/buildStatus: Failed"); then
# echo "=========== BUILD LOG ============"
# kubectl -n ${NS} get lagoonbuilds/${LBUILD} -o yaml
# kubectl logs $(kubectl get pods -n ${NS} --no-headers | awk '{print $1}') -c lagoon-build -n ${NS}
# exit 1
# fi
let CHECK_COUNTER=CHECK_COUNTER+1
echo "Build not running yet"
sleep 30
else
echo "Timeout of $CHECK_TIMEOUT for operator startup reached"
echo "Timeout of $CHECK_TIMEOUT for controller startup reached"
echo "=========== BUILD LOG ============"
kubectl -n ${NS} get lagoonbuilds/${LBUILD} -o yaml
# kubectl -n ${NS} logs lagoon-build-7m5zypx -f
# kubectl logs $(kubectl get pods -n ${NS} --no-headers | awk '{print $1}') -c lagoon-build -n ${NS}
check_operator_log
kubectl -n ${NS} logs ${LBUILD} -f
check_controller_log
tear_down
echo "================ END ================"
exit 1
fi
done
echo "==> Build running"
kubectl -n ${NS} logs lagoon-build-7m5zypx -f
# kubectl -n ${NS} get lagoonbuilds/${LBUILD} -o yaml
kubectl -n ${NS} logs ${LBUILD} -f
}

start_up
start_kind

echo "==> Configure example environment"
echo "====> Install build deploy operator"
if [ ! -z "$BUILD_OPERATOR" ]; then
build_deploy_operator
echo "====> Install build deploy controllers"
if [ ! -z "$BUILD_CONTROLLER" ]; then
build_deploy_controller
else
kubectl create namespace lagoon-builddeploy
helm repo add lagoon-builddeploy https://raw.githubusercontent.com/amazeeio/lagoon-kbd/main/charts
helm upgrade --install -n lagoon-builddeploy lagoon-builddeploy lagoon-builddeploy/lagoon-builddeploy \
--set vars.lagoonTargetName=ci-local-operator-k8s \
--set vars.lagoonTargetName=ci-local-controller-kubernetes \
--set vars.rabbitPassword=guest \
--set vars.rabbitUsername=guest \
--set vars.rabbitHostname=172.17.0.1:5672
Expand All @@ -192,6 +183,6 @@ kubectl -n default apply -f test-resources/example-project1.yaml
sleep 10
check_lagoon_build

check_operator_log
check_controller_log
tear_down
echo "================ END ================"
30 changes: 15 additions & 15 deletions controllers/lagoonbuild_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ Logs on pod %s
}
}

// updateDeploymentAndEnvironmentTask sends the status of the build and deployment to the operatorhandler message queue in lagoon,
// updateDeploymentAndEnvironmentTask sends the status of the build and deployment to the controllerhandler message queue in lagoon,
// this is for the handler in lagoon to process.
func (r *LagoonMonitorReconciler) updateDeploymentAndEnvironmentTask(lagoonBuild *lagoonv1alpha1.LagoonBuild,
jobPod *corev1.Pod,
Expand All @@ -181,7 +181,7 @@ func (r *LagoonMonitorReconciler) updateDeploymentAndEnvironmentTask(lagoonBuild
condition = "cancelled"
}
}
operatorMsg := lagoonv1alpha1.LagoonMessage{
msg := lagoonv1alpha1.LagoonMessage{
Type: "build",
Namespace: lagoonBuild.ObjectMeta.Namespace,
Meta: &lagoonv1alpha1.LagoonLogMeta{
Expand Down Expand Up @@ -211,39 +211,39 @@ func (r *LagoonMonitorReconciler) updateDeploymentAndEnvironmentTask(lagoonBuild
}
}
}
operatorMsg.Meta.Services = serviceNames
msg.Meta.Services = serviceNames
}
// if we aren't being provided the lagoon config, we can skip adding the routes etc
if lagoonEnv != nil {
operatorMsg.Meta.Route = ""
msg.Meta.Route = ""
if route, ok := lagoonEnv.Data["LAGOON_ROUTE"]; ok {
operatorMsg.Meta.Route = route
msg.Meta.Route = route
}
operatorMsg.Meta.Routes = []string{}
msg.Meta.Routes = []string{}
if routes, ok := lagoonEnv.Data["LAGOON_ROUTES"]; ok {
operatorMsg.Meta.Routes = strings.Split(routes, ",")
msg.Meta.Routes = strings.Split(routes, ",")
}
operatorMsg.Meta.MonitoringURLs = []string{}
msg.Meta.MonitoringURLs = []string{}
if monitoringUrls, ok := lagoonEnv.Data["LAGOON_MONITORING_URLS"]; ok {
operatorMsg.Meta.MonitoringURLs = strings.Split(monitoringUrls, ",")
msg.Meta.MonitoringURLs = strings.Split(monitoringUrls, ",")
}
}
// we can add the build start time here
if jobPod.Status.StartTime != nil {
operatorMsg.Meta.StartTime = jobPod.Status.StartTime.Time.UTC().Format("2006-01-02 15:04:05")
msg.Meta.StartTime = jobPod.Status.StartTime.Time.UTC().Format("2006-01-02 15:04:05")
}
// and then once the pod is terminated we can add the terminated time here
if jobPod.Status.ContainerStatuses != nil {
if jobPod.Status.ContainerStatuses[0].State.Terminated != nil {
operatorMsg.Meta.EndTime = jobPod.Status.ContainerStatuses[0].State.Terminated.FinishedAt.Time.UTC().Format("2006-01-02 15:04:05")
msg.Meta.EndTime = jobPod.Status.ContainerStatuses[0].State.Terminated.FinishedAt.Time.UTC().Format("2006-01-02 15:04:05")
}
}
operatorMsgBytes, _ := json.Marshal(operatorMsg)
if err := r.Messaging.Publish("lagoon-tasks:operator", operatorMsgBytes); err != nil {
msgBytes, _ := json.Marshal(msg)
if err := r.Messaging.Publish("lagoon-tasks:controller", msgBytes); err != nil {
// if we can't publish the message, set it as a pending message
// overwrite whatever is there as these are just current state messages so it doesn't
// really matter if we don't smootly transition in what we send back to lagoon
r.updateEnvironmentMessage(context.Background(), lagoonBuild, operatorMsg)
r.updateEnvironmentMessage(context.Background(), lagoonBuild, msg)
return
}
// if we are able to publish the message, then we need to remove any pending messages from the resource
Expand Down Expand Up @@ -274,7 +274,7 @@ func (r *LagoonMonitorReconciler) buildStatusLogsToLagoonLogs(lagoonBuild *lagoo
msg := lagoonv1alpha1.LagoonLog{
Severity: "info",
Project: lagoonBuild.Spec.Project.Name,
Event: "task:builddeploy-kubernetes:" + condition, //@TODO: this probably needs to be changed to a new task event for the operator
Event: "task:builddeploy-kubernetes:" + condition, //@TODO: this probably needs to be changed to a new task event for the controller
Meta: &lagoonv1alpha1.LagoonLogMeta{
ProjectName: lagoonBuild.Spec.Project.Name,
BranchName: lagoonBuild.Spec.Project.Environment,
Expand Down
18 changes: 9 additions & 9 deletions controllers/lagoontask_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ Logs on pod %s
}
}

// updateLagoonTask sends the status of the build and deployment to the operatorhandler message queue in lagoon,
// updateLagoonTask sends the status of the build and deployment to the controllerhandler message queue in lagoon,
// this is for the handler in lagoon to process.
func (r *LagoonMonitorReconciler) updateLagoonTask(lagoonTask *lagoonv1alpha1.LagoonTask,
jobPod *corev1.Pod) {
Expand All @@ -183,7 +183,7 @@ func (r *LagoonMonitorReconciler) updateLagoonTask(lagoonTask *lagoonv1alpha1.La
case corev1.PodSucceeded:
condition = "succeeded"
}
operatorMsg := lagoonv1alpha1.LagoonMessage{
msg := lagoonv1alpha1.LagoonMessage{
Type: "task",
Namespace: lagoonTask.ObjectMeta.Namespace,
Meta: &lagoonv1alpha1.LagoonLogMeta{
Expand All @@ -200,24 +200,24 @@ func (r *LagoonMonitorReconciler) updateLagoonTask(lagoonTask *lagoonv1alpha1.La
// if the task contains `taskData` annotation, this is used to send data back to lagoon
// lagoon will use the data to perform an action against the api or something else
// the data in taskData should be base64 encoded
operatorMsg.Meta.AdvancedData = jobPod.ObjectMeta.Annotations["lagoon.sh/taskData"]
msg.Meta.AdvancedData = jobPod.ObjectMeta.Annotations["lagoon.sh/taskData"]
}
// we can add the build start time here
if jobPod.Status.StartTime != nil {
operatorMsg.Meta.StartTime = jobPod.Status.StartTime.Time.UTC().Format("2006-01-02 15:04:05")
msg.Meta.StartTime = jobPod.Status.StartTime.Time.UTC().Format("2006-01-02 15:04:05")
}
// and then once the pod is terminated we can add the terminated time here
if jobPod.Status.ContainerStatuses != nil {
if jobPod.Status.ContainerStatuses[0].State.Terminated != nil {
operatorMsg.Meta.EndTime = jobPod.Status.ContainerStatuses[0].State.Terminated.FinishedAt.Time.UTC().Format("2006-01-02 15:04:05")
msg.Meta.EndTime = jobPod.Status.ContainerStatuses[0].State.Terminated.FinishedAt.Time.UTC().Format("2006-01-02 15:04:05")
}
}
operatorMsgBytes, _ := json.Marshal(operatorMsg)
if err := r.Messaging.Publish("lagoon-tasks:operator", operatorMsgBytes); err != nil {
msgBytes, _ := json.Marshal(msg)
if err := r.Messaging.Publish("lagoon-tasks:controller", msgBytes); err != nil {
// if we can't publish the message, set it as a pending message
// overwrite whatever is there as these are just current state messages so it doesn't
// really matter if we don't smootly transition in what we send back to lagoon
r.updateTaskEnvironmentMessage(context.Background(), lagoonTask, operatorMsg)
r.updateTaskEnvironmentMessage(context.Background(), lagoonTask, msg)
return
}
// if we are able to publish the message, then we need to remove any pending messages from the resource
Expand All @@ -242,7 +242,7 @@ func (r *LagoonMonitorReconciler) taskStatusLogsToLagoonLogs(lagoonTask *lagoonv
msg := lagoonv1alpha1.LagoonLog{
Severity: "info",
Project: lagoonTask.Spec.Project.Name,
Event: "task:job-kubernetes:" + condition, //@TODO: this probably needs to be changed to a new task event for the operator
Event: "task:job-kubernetes:" + condition, //@TODO: this probably needs to be changed to a new task event for the controller
Meta: &lagoonv1alpha1.LagoonLogMeta{
Task: &lagoonTask.Spec.Task,
ProjectName: lagoonTask.Spec.Project.Name,
Expand Down
10 changes: 5 additions & 5 deletions handlers/message_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func NewMessaging(config mq.Config, client client.Client, startupAttempts int, s
}
}

// Consumer handles consuming messages sent to the queue that this operator is connected to and processes them accordingly
// Consumer handles consuming messages sent to the queue that these controllers are connected to and processes them accordingly
func (h *Messaging) Consumer(targetName string) { //error {
opLog := ctrl.Log.WithName("handlers").WithName("LagoonTasks")
var messageQueue mq.MQ
Expand Down Expand Up @@ -175,16 +175,16 @@ func (h *Messaging) Consumer(targetName string) { //error {
removeTask.OpenshiftProjectName,
),
)
operatorMsg := lagoonv1alpha1.LagoonMessage{
msg := lagoonv1alpha1.LagoonMessage{
Type: "remove",
Namespace: removeTask.OpenshiftProjectName,
Meta: &lagoonv1alpha1.LagoonLogMeta{
Project: removeTask.ProjectName,
Environment: removeTask.Branch,
},
}
operatorMsgBytes, _ := json.Marshal(operatorMsg)
h.Publish("lagoon-tasks:operator", operatorMsgBytes)
msgBytes, _ := json.Marshal(msg)
h.Publish("lagoon-tasks:controller", msgBytes)
}
message.Ack(false) // ack to remove from queue
})
Expand Down Expand Up @@ -373,7 +373,7 @@ func (h *Messaging) GetPendingMessages() {
}
}
if build.StatusMessages.EnvironmentMessage != nil {
if err := h.Publish("lagoon-tasks:operator", envBytes); err != nil {
if err := h.Publish("lagoon-tasks:controller", envBytes); err != nil {
opLog.Info(fmt.Sprintf("Unable to publush message: %v", err))
break
}
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,9 @@ func main() {
},
},
{
Name: "lagoon-tasks:operator",
Name: "lagoon-tasks:controller",
Exchange: "lagoon-tasks",
RoutingKey: "operator",
RoutingKey: "controller",
Options: mq.Options{
"app_id": lagoonAppID,
"delivery_mode": "2",
Expand Down

0 comments on commit e96fe17

Please sign in to comment.