Skip to content

Commit

Permalink
feat: specify --kubernetes-labels for agent (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaspin authored Jan 17, 2024
1 parent 446e87d commit 3351c67
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 10 deletions.
25 changes: 25 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,41 @@ func buildConfig(endpoint string) (*controller.Config, error) {
agentStartupParameters = strings.Split(os.Getenv("SEMAPHORE_AGENT_STARTUP_PARAMETERS"), " ")
}

labels, err := parseLabels()
if err != nil {
return nil, fmt.Errorf("unable to determine labels")
}

return &controller.Config{
SemaphoreEndpoint: endpoint,
Namespace: k8sNamespace,
ServiceAccountName: svcAccountName,
AgentImage: agentImage,
AgentStartupParameters: agentStartupParameters,
MaxParallelJobs: maxParallelJobs,
Labels: labels,
}, nil
}

func parseLabels() ([]string, error) {
labels := []string{}
fromEnv := os.Getenv("SEMAPHORE_AGENT_LABELS")
if fromEnv == "" {
return labels, nil
}

for _, label := range strings.Split(fromEnv, ",") {
parts := strings.Split(label, "=")
if len(parts) != 2 {
return nil, fmt.Errorf("%s is not a valid label", label)
}

labels = append(labels, label)
}

return labels, nil
}

func newK8sClientset() (kubernetes.Interface, error) {
clientset, err := newInClusterClientset()
if err != nil {
Expand Down
44 changes: 34 additions & 10 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log"
"strings"
"time"

"github.com/renderedtext/agent-k8s-stack/pkg/semaphore"
Expand All @@ -19,6 +20,7 @@ type Config struct {
ServiceAccountName string
AgentImage string
AgentStartupParameters []string
Labels []string
MaxParallelJobs int
SemaphoreEndpoint string
}
Expand Down Expand Up @@ -255,11 +257,22 @@ func (c *Controller) buildJob(job semaphore.JobRequest, agentTypes []*AgentType)
},
},
{
Name: "KUBERNETES_POD_NAME",
Name: "SEMAPHORE_AGENT_NAME",
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{FieldPath: "metadata.name"},
},
},
{
Name: "SEMAPHORE_AGENT_TOKEN",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
Key: "registrationToken",
LocalObjectReference: corev1.LocalObjectReference{
Name: agentType.SecretName,
},
},
},
},
},
},
},
Expand All @@ -270,25 +283,36 @@ func (c *Controller) buildJob(job semaphore.JobRequest, agentTypes []*AgentType)
}

func (c *Controller) buildLabels(job semaphore.JobRequest) map[string]string {
return map[string]string{
"app": "semaphore",
labels := map[string]string{
"semaphoreci.com/agent-type": job.MachineType,
}

for _, label := range c.cfg.Labels {
parts := strings.Split(label, "=")
labels[parts[0]] = parts[1]
}

return labels
}

// TODO: do not pass registration token in plain text like this, use environment variable
func (c *Controller) buildAgentStartupParameters(agentType *AgentType, jobID string) []string {
labels := []string{
fmt.Sprintf("semaphoreci.com/agent-type=%s", agentType.AgentTypeName),
}

if len(c.cfg.Labels) > 0 {
labels = append(labels, c.cfg.Labels...)
}

parameters := []string{
"--kubernetes-executor",
"--disconnect-after-job",
"--name-from-env",
"KUBERNETES_POD_NAME",
"--endpoint",
c.cfg.SemaphoreEndpoint,
"--token",
agentType.RegistrationToken,
"--job-id",
jobID,
"--kubernetes-labels",
strings.Join(labels, ","),
"--kubernetes-executor",
"--disconnect-after-job",
}

// If agent type does not specify startup parameters, use the controller's defaults.
Expand Down

0 comments on commit 3351c67

Please sign in to comment.