Skip to content

Commit

Permalink
(feat) Set Status of version on custom resource (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
peimanja authored Apr 5, 2022
1 parent 7ddc447 commit ce7a05e
Show file tree
Hide file tree
Showing 11 changed files with 743 additions and 21 deletions.
46 changes: 46 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: CI

on:
pull_request:
branches:
- main
paths-ignore:
- .github/workflows/publish-helm-chart.yaml
- .github/workflows/publish-images.yaml
- .github/workflows/release-drafter.yaml
- '**.md'
- '.gitignore'

jobs:
test:
runs-on: ubuntu-latest
name: Test
steps:
- name: Checkout
uses: actions/checkout@v3

- uses: actions/setup-go@v3
with:
go-version: '1.16.x'

- run: go version
- uses: actions/cache@v3
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Install kubebuilder
run: |
curl -L -O https://github.com/kubernetes-sigs/kubebuilder/releases/download/v2.3.2/kubebuilder_2.3.2_linux_amd64.tar.gz
tar zxvf kubebuilder_2.3.2_linux_amd64.tar.gz
sudo mv kubebuilder_2.3.2_linux_amd64 /usr/local/kubebuilder
- name: Run tests
run: make test

- name: Verify manifests are up-to-date
run: |
make manifests
git diff --exit-code
72 changes: 72 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,75 @@ GOBIN=$(PROJECT_DIR)/bin go get $(2) ;\
rm -rf $$TMP_DIR ;\
}
endef

# find or download etcd
etcd:
ifeq (, $(shell which etcd))
ifeq (, $(wildcard $(TEST_ASSETS)/etcd))
@{ \
set -xe ;\
INSTALL_TMP_DIR=$$(mktemp -d) ;\
cd $$INSTALL_TMP_DIR ;\
wget https://github.com/kubernetes-sigs/kubebuilder/releases/download/v2.3.2/kubebuilder_2.3.2_$(OS_NAME)_amd64.tar.gz ;\
mkdir -p $(TEST_ASSETS) ;\
tar zxvf kubebuilder_2.3.2_$(OS_NAME)_amd64.tar.gz ;\
mv kubebuilder_2.3.2_$(OS_NAME)_amd64/bin/etcd $(TEST_ASSETS)/etcd ;\
mv kubebuilder_2.3.2_$(OS_NAME)_amd64/bin/kube-apiserver $(TEST_ASSETS)/kube-apiserver ;\
mv kubebuilder_2.3.2_$(OS_NAME)_amd64/bin/kubectl $(TEST_ASSETS)/kubectl ;\
rm -rf $$INSTALL_TMP_DIR ;\
}
ETCD_BIN=$(TEST_ASSETS)/etcd
else
ETCD_BIN=$(TEST_ASSETS)/etcd
endif
else
ETCD_BIN=$(shell which etcd)
endif

# find or download kube-apiserver
kube-apiserver:
ifeq (, $(shell which kube-apiserver))
ifeq (, $(wildcard $(TEST_ASSETS)/kube-apiserver))
@{ \
set -xe ;\
INSTALL_TMP_DIR=$$(mktemp -d) ;\
cd $$INSTALL_TMP_DIR ;\
wget https://github.com/kubernetes-sigs/kubebuilder/releases/download/v2.3.2/kubebuilder_2.3.2_$(OS_NAME)_amd64.tar.gz ;\
mkdir -p $(TEST_ASSETS) ;\
tar zxvf kubebuilder_2.3.2_$(OS_NAME)_amd64.tar.gz ;\
mv kubebuilder_2.3.2_$(OS_NAME)_amd64/bin/etcd $(TEST_ASSETS)/etcd ;\
mv kubebuilder_2.3.2_$(OS_NAME)_amd64/bin/kube-apiserver $(TEST_ASSETS)/kube-apiserver ;\
mv kubebuilder_2.3.2_$(OS_NAME)_amd64/bin/kubectl $(TEST_ASSETS)/kubectl ;\
rm -rf $$INSTALL_TMP_DIR ;\
}
KUBE_APISERVER_BIN=$(TEST_ASSETS)/kube-apiserver
else
KUBE_APISERVER_BIN=$(TEST_ASSETS)/kube-apiserver
endif
else
KUBE_APISERVER_BIN=$(shell which kube-apiserver)
endif

# find or download kubectl
kubectl:
ifeq (, $(shell which kubectl))
ifeq (, $(wildcard $(TEST_ASSETS)/kubectl))
@{ \
set -xe ;\
INSTALL_TMP_DIR=$$(mktemp -d) ;\
cd $$INSTALL_TMP_DIR ;\
wget https://github.com/kubernetes-sigs/kubebuilder/releases/download/v2.3.2/kubebuilder_2.3.2_$(OS_NAME)_amd64.tar.gz ;\
mkdir -p $(TEST_ASSETS) ;\
tar zxvf kubebuilder_2.3.2_$(OS_NAME)_amd64.tar.gz ;\
mv kubebuilder_2.3.2_$(OS_NAME)_amd64/bin/etcd $(TEST_ASSETS)/etcd ;\
mv kubebuilder_2.3.2_$(OS_NAME)_amd64/bin/kube-apiserver $(TEST_ASSETS)/kube-apiserver ;\
mv kubebuilder_2.3.2_$(OS_NAME)_amd64/bin/kubectl $(TEST_ASSETS)/kubectl ;\
rm -rf $$INSTALL_TMP_DIR ;\
}
KUBECTL_BIN=$(TEST_ASSETS)/kubectl
else
KUBECTL_BIN=$(TEST_ASSETS)/kubectl
endif
else
KUBECTL_BIN=$(shell which kubectl)
endif
51 changes: 41 additions & 10 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package agent

import (
"context"
"reflect"
"time"

"github.com/go-logr/logr"
Expand Down Expand Up @@ -46,6 +47,9 @@ func (r *VersionTrackerReconciler) Reconcile(ctx context.Context, req ctrl.Reque

log.Info("starting reconciliation", "interval", r.Config.Interval)
var v v1alpha1.VersionTracker
var status v1alpha1.VersionTrackerStatus
var sv SubjectVersion

if err := r.Get(ctx, req.NamespacedName, &v); err != nil {
log.Error(err, "unable to fetch VersionTracker")
reconciliationErrorsTotal.Inc()
Expand Down Expand Up @@ -92,29 +96,56 @@ func (r *VersionTrackerReconciler) Reconcile(ctx context.Context, req ctrl.Reque
return ctrl.Result{}, err
}

status.ID = &v.Spec.Name
status.Namespace = &v.ObjectMeta.Namespace
status.LocalVersion = &v.Spec.LocalVersion
status.RemoteVersion = &v.Spec.RemoteVersion

// Get items based on the resource type
items := GetItems(resources)
if len(items) == 0 {
log.Info("no resources found")
count := 0
status.TotalResourceCount = &count
} else {
// Extract versions from resources
sv := r.ExtractSubjectVersion(v, items)

// Ship the version information to the Control Plane
if len(sv.Versions) > 0 && r.Config.ControlPlaneUrl != "" {
err := r.ShipToControlPlane(sv)
if err != nil {
log.Error(err, "failed to ship the version to control plane")
reconciliationErrorsTotal.Inc()
return ctrl.Result{}, err
}
sv = r.ExtractSubjectVersion(v, items)
var uniqVersions []*string
for _, v := range sv.UniqVersions {
uniqVersions = append(uniqVersions, &v)
}
status.TotalResourceCount = &sv.TotalResourceCount
status.UniqVersions = uniqVersions
status.Versions = sv.Versions
}

// Update the VersionTracker status
if !reflect.DeepEqual(v.Status, status) {
updated := v.DeepCopy()
updated.Status = status
if err := r.Status().Patch(ctx, updated, client.MergeFrom(&v)); err != nil {
log.Info("Failed to patch VersionTracker", "error", err)
return ctrl.Result{
Requeue: true,
}, nil
}
}

// Ship the version information to the Control Plane
if len(sv.Versions) > 0 && r.Config.ControlPlaneUrl != "" {
err := r.ShipToControlPlane(sv)
if err != nil {
log.Error(err, "failed to ship the version to control plane")
reconciliationErrorsTotal.Inc()
return ctrl.Result{}, err
}
}

elapsed := time.Since(start)
lastReconciliationTimestamp.SetToCurrentTime()
reconciliationDuration.Set(float64(elapsed.Milliseconds()))
log.Info("done reconciling", "interval", r.Config.Interval)

return ctrl.Result{
RequeueAfter: r.Config.Interval,
}, nil
Expand Down
14 changes: 14 additions & 0 deletions agent/api/v1alpha1/versiontracker_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,22 @@ type Regex struct {
Result string `json:"result"`
}

type Version struct {
ResourceCount int `json:"resourceCount"`
ResourceKind string `json:"resourceKind"`
ExtractedFrom string `json:"extractedFrom"`
Version string `json:"version"`
}

// VersionTrackerStatus defines the observed state of VersionTracker
type VersionTrackerStatus struct {
ID *string `json:"id,omitempty"`
Namespace *string `json:"namespace,omitempty"`
TotalResourceCount *int `json:"totalResourceCount,omitempty"`
UniqVersions []*string `json:"uniqVersions,omitempty"`
Versions []*Version `json:"versions,omitempty"`
LocalVersion *LocalVersion `json:"localVersion,omitempty"`
RemoteVersion *RemoteVersion `json:"remoteVersion,omitempty"`
}

//+kubebuilder:object:root=true
Expand Down
64 changes: 63 additions & 1 deletion agent/api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 2 additions & 9 deletions agent/extract_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,10 @@ type SubjectVersion struct {
Namespace string
TotalResourceCount int
UniqVersions []string
Versions []*Version
Versions []*v1alpha1.Version
RemoteVersion v1alpha1.RemoteVersion
}

type Version struct {
ResourceCount int
ResourceKind string
ExtractedFrom string
Version string
}

// ExtractSubjectVersion looks at the feild of each individuel resource and extracts the version
// based on the extraction configuration in the VersionTracker
func (r *VersionTrackerReconciler) ExtractSubjectVersion(v v1alpha1.VersionTracker, items []interface{}) SubjectVersion {
Expand Down Expand Up @@ -77,7 +70,7 @@ func (r *VersionTrackerReconciler) ExtractSubjectVersion(v v1alpha1.VersionTrack
// add the version to the list of unique versions if it's not already there
if !utils.Contains(uniqueVersions, version) {
uniqueVersions = append(uniqueVersions, version)
appVersion.Versions = append(appVersion.Versions, &Version{
appVersion.Versions = append(appVersion.Versions, &v1alpha1.Version{
Version: version,
ExtractedFrom: fieldValue,
ResourceKind: v.GetResourceKind(),
Expand Down
Loading

0 comments on commit ce7a05e

Please sign in to comment.