
This commit removes the configuration files for Godeps as well as the vendored dependencies, replacing them with go modules, Go's built-in dependency management system. This dramatically slims down the size of the repo (from 25M to 324K, discounting the .git directory) and greatly speeds up cloning times. This will also provide mechanisms for managing versions of any auxiliary tools (e.g. linters), creating a reproducible environment for developers and CI/CD efforts. This also modifies the Makefile to take into account that the repo no longer needs to be cloned into the GOPATH. Change-Id: I2213792cc3ce81831d5b835f2252ca6f137e0086
90 lines
2.1 KiB
Go
90 lines
2.1 KiB
Go
package job
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
v1 "k8s.io/api/batch/v1"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
entry "opendev.org/airship/kubernetes-entrypoint/entrypoint"
|
|
"opendev.org/airship/kubernetes-entrypoint/logger"
|
|
"opendev.org/airship/kubernetes-entrypoint/util/env"
|
|
)
|
|
|
|
const FailingStatusFormat = "Job %s is not completed yet"
|
|
|
|
type Job struct {
|
|
name string
|
|
namespace string
|
|
labels map[string]string
|
|
}
|
|
|
|
func init() {
|
|
jobsEnv := fmt.Sprintf("%sJOBS", entry.DependencyPrefix)
|
|
jobsJsonEnv := fmt.Sprintf("%s%s", jobsEnv, entry.JsonSuffix)
|
|
if jobsDeps := env.SplitJobEnvToDeps(jobsEnv, jobsJsonEnv); jobsDeps != nil {
|
|
if len(jobsDeps) > 0 {
|
|
for _, dep := range jobsDeps {
|
|
job := NewJob(dep.Name, dep.Namespace, dep.Labels)
|
|
if job != nil {
|
|
entry.Register(*job)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func NewJob(name string, namespace string, labels map[string]string) *Job {
|
|
if name != "" && labels != nil {
|
|
logger.Warning.Printf("Cannot specify both name and labels for job depependency")
|
|
return nil
|
|
}
|
|
return &Job{
|
|
name: name,
|
|
namespace: namespace,
|
|
labels: labels,
|
|
}
|
|
}
|
|
|
|
func (j Job) IsResolved(entrypoint entry.EntrypointInterface) (bool, error) {
|
|
iface := entrypoint.Client().Jobs(j.namespace)
|
|
var jobs []v1.Job
|
|
|
|
if j.name != "" {
|
|
job, err := iface.Get(j.name, metav1.GetOptions{})
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
jobs = []v1.Job{*job}
|
|
} else if j.labels != nil {
|
|
labelSelector := &metav1.LabelSelector{MatchLabels: j.labels}
|
|
label := metav1.FormatLabelSelector(labelSelector)
|
|
opts := metav1.ListOptions{LabelSelector: label}
|
|
jobList, err := iface.List(opts)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
jobs = jobList.Items
|
|
}
|
|
if len(jobs) == 0 {
|
|
return false, fmt.Errorf("No matching jobs found: %v", j)
|
|
}
|
|
|
|
for _, job := range jobs {
|
|
if job.Status.Succeeded == 0 {
|
|
return false, fmt.Errorf(FailingStatusFormat, j)
|
|
}
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
func (j Job) String() string {
|
|
prefix := "Jobs"
|
|
if j.name != "" {
|
|
prefix = fmt.Sprintf("Job %s", j.name)
|
|
} else if j.labels != nil {
|
|
prefix = fmt.Sprintf("Jobs with labels %s", j.labels)
|
|
}
|
|
return fmt.Sprintf("%s in namespace %s", prefix, j.namespace)
|
|
}
|