Skip to content

Commit

Permalink
STONEBLD-2133 Update pooled hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartwdouglas committed Feb 2, 2024
1 parent 0fb07f0 commit 1b28511
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 1 deletion.
1 change: 1 addition & 0 deletions deploy/operator/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ resources:
- provision-shared-host.yaml
- clean-shared-host.yaml
- openshift-specific-rbac.yaml
- update-host.yaml
31 changes: 31 additions & 0 deletions deploy/operator/update-host.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: update-host
namespace: multi-platform-controller
spec:
description: >-
This task will create a new user on a host, setup ssh keys, and then create the relevant secret.
params:
- name: HOST
type: string
- name: USER
type: string
workspaces:
- name: ssh
steps:
- name: provision
image: quay.io/redhat-appstudio/multi-platform-runner:01c7670e81d5120347cf0ad13372742489985e5f@sha256:246adeaaba600e207131d63a7f706cffdcdc37d8f600c56187123ec62823ff44
imagePullPolicy: IfNotPresent
script: |
#!/bin/bash
cd /tmp
set -o verbose
set -eu
set -o pipefail
mkdir -p /root/.ssh
cp $(workspaces.ssh.path)/id_rsa /tmp/master_key
chmod 0400 /tmp/master_key
export SSH_HOST=$(params.USER)@$(params.HOST)
ssh -i /tmp/master_key -o StrictHostKeyChecking=no $SSH_HOST "sudo dnf update -y"
2 changes: 1 addition & 1 deletion deploy/overlays/dev-template/host-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ metadata:
name: host-config
namespace: multi-platform-controller
data:
dynamic-platforms: linux/arm64,linux/s390x,linux/ppc64le,linux/amd64
dynamic-platforms: linux/arm64,linux/amd64
instance-tag: QUAY_USERNAME-development

dynamic.linux-arm64.type: aws
Expand Down
13 changes: 13 additions & 0 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,18 @@ func NewManager(cfg *rest.Config, options ctrl.Options) (ctrl.Manager, error) {
return nil, err
}

ticker := time.NewTicker(time.Hour * 24)
go func() {
for range ticker.C {
taskrun.UpdateHostPools(operatorNamespace, mgr.GetClient(), &controllerLog)
}
}()
timer := time.NewTimer(time.Minute)
go func() {
<-timer.C
//update the nodes on startup
taskrun.UpdateHostPools(operatorNamespace, mgr.GetClient(), &controllerLog)
}()

return mgr, nil
}
5 changes: 5 additions & 0 deletions pkg/reconciler/taskrun/taskrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const (

TaskTypeLabel = "build.appstudio.redhat.com/task-type"
TaskTypeProvision = "provision"
TaskTypeUpdate = "update"
TaskTypeClean = "clean"

ServiceAccountName = "multi-platform-controller"
Expand Down Expand Up @@ -125,6 +126,10 @@ func (r *ReconcileTaskRun) handleTaskRunReceived(ctx context.Context, log *logr.
log.Info("Reconciling provision task")
return r.handleProvisionTask(ctx, log, tr)
}
if taskType == TaskTypeUpdate {
// We don't care about these
return reconcile.Result{}, nil

Check warning on line 131 in pkg/reconciler/taskrun/taskrun.go

View check run for this annotation

Codecov / codecov/patch

pkg/reconciler/taskrun/taskrun.go#L131

Added line #L131 was not covered by tests
}
}
if tr.Spec.Params == nil {
return reconcile.Result{}, nil
Expand Down
92 changes: 92 additions & 0 deletions pkg/reconciler/taskrun/updates.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package taskrun

import (
"context"
"github.com/go-logr/logr"
v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
v12 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"strings"
"time"
)

// UpdateHostPools Run the host update task periodically
func UpdateHostPools(operatorNamespace string, client client.Client, log *logr.Logger) {
log.Info("running pooled host update")
cm := v12.ConfigMap{}
err := client.Get(context.Background(), types.NamespacedName{Namespace: operatorNamespace, Name: HostConfig}, &cm)
if err != nil {
log.Error(err, "Failed to read config to update hosts", "audit", "true")
return

Check warning on line 22 in pkg/reconciler/taskrun/updates.go

View check run for this annotation

Codecov / codecov/patch

pkg/reconciler/taskrun/updates.go#L16-L22

Added lines #L16 - L22 were not covered by tests
}

hosts := map[string]*Host{}
for k, v := range cm.Data {
if !strings.HasPrefix(k, "host.") {
continue

Check warning on line 28 in pkg/reconciler/taskrun/updates.go

View check run for this annotation

Codecov / codecov/patch

pkg/reconciler/taskrun/updates.go#L25-L28

Added lines #L25 - L28 were not covered by tests
}
k = k[len("host."):]
pos := strings.LastIndex(k, ".")
if pos == -1 {
continue

Check warning on line 33 in pkg/reconciler/taskrun/updates.go

View check run for this annotation

Codecov / codecov/patch

pkg/reconciler/taskrun/updates.go#L30-L33

Added lines #L30 - L33 were not covered by tests
}
name := k[0:pos]
key := k[pos+1:]
host := hosts[name]
if host == nil {
host = &Host{}
hosts[name] = host
host.Name = name

Check warning on line 41 in pkg/reconciler/taskrun/updates.go

View check run for this annotation

Codecov / codecov/patch

pkg/reconciler/taskrun/updates.go#L35-L41

Added lines #L35 - L41 were not covered by tests
}
switch key {
case "address":
host.Address = v
case "user":
host.User = v
case "platform":
host.Platform = v
case "secret":
host.Secret = v
case "concurrency":
default:
log.Info("unknown key", "key", key)

Check warning on line 54 in pkg/reconciler/taskrun/updates.go

View check run for this annotation

Codecov / codecov/patch

pkg/reconciler/taskrun/updates.go#L43-L54

Added lines #L43 - L54 were not covered by tests
}
}
delay := 0
for hostName := range hosts {
log.Info("scheduling host update", "host", hostName)

Check warning on line 59 in pkg/reconciler/taskrun/updates.go

View check run for this annotation

Codecov / codecov/patch

pkg/reconciler/taskrun/updates.go#L57-L59

Added lines #L57 - L59 were not covered by tests
// We don't want to run all updates at once
// Stagger all updates by 10 minutes
timer := time.NewTimer(time.Minute * time.Duration(delay) * 10)
delay++
realHostName := hostName
host := hosts[realHostName]
go func() {
<-timer.C

Check warning on line 67 in pkg/reconciler/taskrun/updates.go

View check run for this annotation

Codecov / codecov/patch

pkg/reconciler/taskrun/updates.go#L62-L67

Added lines #L62 - L67 were not covered by tests

log.Info("updating host", "host", realHostName)
provision := v1.TaskRun{}
provision.GenerateName = "update-task"
provision.Namespace = operatorNamespace
provision.Labels = map[string]string{TaskTypeLabel: TaskTypeUpdate, AssignedHost: realHostName}
provision.Spec.TaskRef = &v1.TaskRef{Name: "update-host"}
provision.Spec.Workspaces = []v1.WorkspaceBinding{{Name: "ssh", Secret: &v12.SecretVolumeSource{SecretName: host.Secret}}}
compute := map[v12.ResourceName]resource.Quantity{v12.ResourceCPU: resource.MustParse("100m"), v12.ResourceMemory: resource.MustParse("256Mi")}
provision.Spec.ComputeResources = &v12.ResourceRequirements{Requests: compute, Limits: compute}
provision.Spec.ServiceAccountName = ServiceAccountName //TODO: special service account for this
provision.Spec.Params = []v1.Param{

Check warning on line 79 in pkg/reconciler/taskrun/updates.go

View check run for this annotation

Codecov / codecov/patch

pkg/reconciler/taskrun/updates.go#L69-L79

Added lines #L69 - L79 were not covered by tests
{
Name: "HOST",
Value: *v1.NewStructuredValues(host.Address),
},

Check warning on line 83 in pkg/reconciler/taskrun/updates.go

View check run for this annotation

Codecov / codecov/patch

pkg/reconciler/taskrun/updates.go#L81-L83

Added lines #L81 - L83 were not covered by tests
{
Name: "USER",
Value: *v1.NewStructuredValues(host.User),
},

Check warning on line 87 in pkg/reconciler/taskrun/updates.go

View check run for this annotation

Codecov / codecov/patch

pkg/reconciler/taskrun/updates.go#L85-L87

Added lines #L85 - L87 were not covered by tests
}
err = client.Create(context.Background(), &provision)
}()

Check warning on line 90 in pkg/reconciler/taskrun/updates.go

View check run for this annotation

Codecov / codecov/patch

pkg/reconciler/taskrun/updates.go#L89-L90

Added lines #L89 - L90 were not covered by tests
}
}

0 comments on commit 1b28511

Please sign in to comment.