Skip to content

Commit

Permalink
checkpoint
Browse files Browse the repository at this point in the history
Signed-off-by: Jeff Ortel <[email protected]>
  • Loading branch information
jortel committed Feb 7, 2024
1 parent be0e1b0 commit 1669f08
Show file tree
Hide file tree
Showing 6 changed files with 3,753 additions and 128 deletions.
2 changes: 1 addition & 1 deletion api/addon.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,5 @@ type Addon struct {
// With model.
func (r *Addon) With(m *crd.Addon) {
r.Name = m.Name
r.Image = m.Spec.Image
r.Image = m.Spec.Main.Image
}
3,722 changes: 3,643 additions & 79 deletions generated/crd/tackle.konveyor.io_addons.yaml

Large diffs are not rendered by default.

17 changes: 9 additions & 8 deletions k8s/api/tackle/v1alpha1/addon.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,12 @@ import (

// AddonSpec defines the desired state of Addon
type AddonSpec struct {
// Addon fqin.
Image string `json:"image"`
// ImagePullPolicy an optional image pull policy.
// +kubebuilder:default=IfNotPresent
// +kubebuilder:validation:Enum=IfNotPresent;Always;Never
ImagePullPolicy core.PullPolicy `json:"imagePullPolicy,omitempty"`
// Resource requirements.
Resources core.ResourceRequirements `json:"resources,omitempty"`
// Main container.
Main core.Container `json:"main"`
// Init containers.
Init []core.Container `json:"init,omitempty"`
// Support containers.
Support []core.Container `json:"support,omitempty"`
}

// AddonStatus defines the observed state of Addon
Expand All @@ -40,19 +38,22 @@ type AddonStatus struct {
ObservedGeneration int64 `json:"observedGeneration,omitempty"`
}

// Addon CR
// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +k8s:openapi-gen=true
// +kubebuilder:subresource:status
// +kubebuilder:printcolumn:name="READY",type=string,JSONPath=".status.conditions[?(@.type=='Ready')].status"
// +kubebuilder:printcolumn:name="AGE",type="date",JSONPath=".metadata.creationTimestamp"
//
type Addon struct {
meta.TypeMeta `json:",inline"`
meta.ObjectMeta `json:"metadata,omitempty"`
Spec AddonSpec `json:"spec,omitempty"`
Status AddonStatus `json:"status,omitempty"`
}

// AddonList CR
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
type AddonList struct {
meta.TypeMeta `json:",inline"`
Expand Down
17 changes: 16 additions & 1 deletion k8s/api/tackle/v1alpha1/zz_generated.deepcopy.go

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

7 changes: 7 additions & 0 deletions settings/addon.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ import (
)

const (
EnvRootDir = "ROOT"
EnvHubBaseURL = "HUB_BASE_URL"
EnvHubToken = "TOKEN"
EnvTask = "TASK"
)

// Addon settings.
type Addon struct {
// RootDir addon root directory.
RootDir string
// Hub settings.
Hub struct {
// URL for the hub API.
Expand All @@ -27,6 +30,10 @@ type Addon struct {

func (r *Addon) Load() (err error) {
var found bool
r.RootDir, found = os.LookupEnv(EnvRootDir)
if !found {
r.RootDir = "/addon"
}
r.Hub.URL, found = os.LookupEnv(EnvHubBaseURL)
if !found {
r.Hub.URL = "http://localhost:8080"
Expand Down
116 changes: 77 additions & 39 deletions task/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ const (
Unit = time.Second
)

const (
Shared = "shared"
Cache = "cache"
)

var (
Settings = &settings.Settings
Log = logr.WithName("task-scheduler")
Expand Down Expand Up @@ -273,7 +278,7 @@ func (r *Task) Run(client k8s.Client) (err error) {
if err != nil {
return
}
r.Image = addon.Spec.Image
r.Image = addon.Spec.Main.Image
secret := r.secret(addon)
err = client.Create(context.TODO(), &secret)
if err != nil {
Expand Down Expand Up @@ -415,6 +420,7 @@ func (r *Task) Cancel(client k8s.Client) (err error) {

// findAddon by name.
func (r *Task) findAddon(client k8s.Client, name string) (addon *crd.Addon, err error) {
// current
addon = &crd.Addon{}
err = client.Get(
context.TODO(),
Expand Down Expand Up @@ -478,8 +484,14 @@ func (r *Task) pod(addon *crd.Addon, owner *crd.Tackle, secret *core.Secret) (po

// specification builds a Pod specification.
func (r *Task) specification(addon *crd.Addon, secret *core.Secret) (specification core.PodSpec) {
shared := core.Volume{
Name: Shared,
VolumeSource: core.VolumeSource{
EmptyDir: &core.EmptyDirVolumeSource{},
},
}
cache := core.Volume{
Name: "cache",
Name: Cache,
}
if Settings.Cache.RWX {
cache.VolumeSource = core.VolumeSource{
Expand All @@ -495,61 +507,87 @@ func (r *Task) specification(addon *crd.Addon, secret *core.Secret) (specificati
specification = core.PodSpec{
ServiceAccountName: Settings.Hub.Task.SA,
RestartPolicy: core.RestartPolicyNever,
Containers: []core.Container{
r.container(addon, secret),
},
Containers: r.containers(addon, secret),
Volumes: []core.Volume{
shared,
cache,
},
}

return
}

// container builds the pod container.
func (r *Task) container(addon *crd.Addon, secret *core.Secret) (container core.Container) {
// containers builds the pod containers.
func (r *Task) containers(addon *crd.Addon, secret *core.Secret) (containers []core.Container) {
userid := int64(0)
policy := core.PullIfNotPresent
if addon.Spec.ImagePullPolicy != "" {
policy = addon.Spec.ImagePullPolicy
token := &core.EnvVarSource{
SecretKeyRef: &core.SecretKeySelector{
Key: settings.EnvHubToken,
LocalObjectReference: core.LocalObjectReference{
Name: secret.Name,
},
},
}
containers = []core.Container{
r.mainContainer(addon),
}
container = core.Container{
Name: "main",
Image: r.Image,
ImagePullPolicy: policy,
Resources: addon.Spec.Resources,
Env: []core.EnvVar{
{
containers = append(
containers,
r.initContainers(addon)...)
containers = append(
containers,
r.supportContainers(addon)...)
for i := range containers {
cont := &containers[i]
if cont.ImagePullPolicy == "" {
cont.ImagePullPolicy = core.PullAlways
}
cont.SecurityContext = &core.SecurityContext{
RunAsUser: &userid,
}
cont.VolumeMounts = append(
cont.VolumeMounts,
core.VolumeMount{
Name: Shared,
MountPath: Settings.Addon.RootDir,
},
core.VolumeMount{
Name: Cache,
MountPath: Settings.Cache.Path,
})
cont.Env = append(
cont.Env,
core.EnvVar{
Name: settings.EnvHubBaseURL,
Value: Settings.Addon.Hub.URL,
},
{
core.EnvVar{
Name: settings.EnvTask,
Value: strconv.Itoa(int(r.Task.ID)),
},
{
Name: settings.EnvHubToken,
ValueFrom: &core.EnvVarSource{
SecretKeyRef: &core.SecretKeySelector{
Key: settings.EnvHubToken,
LocalObjectReference: core.LocalObjectReference{
Name: secret.Name,
},
},
},
},
},
VolumeMounts: []core.VolumeMount{
{
Name: "cache",
MountPath: Settings.Cache.Path,
},
},
SecurityContext: &core.SecurityContext{
RunAsUser: &userid,
},
core.EnvVar{
Name: settings.EnvHubToken,
ValueFrom: token,
})
}
return
}

// mainContainer builds the pod container.
func (r *Task) mainContainer(addon *crd.Addon) (main core.Container) {
main = addon.Spec.Main
return
}

// initContainers builds the pod init containers.
func (r *Task) initContainers(addon *crd.Addon) (init []core.Container) {
init = addon.Spec.Init
return
}

// supportContainers builds the pod support containers.
func (r *Task) supportContainers(addon *crd.Addon) (init []core.Container) {
init = addon.Spec.Support
return
}

Expand Down

0 comments on commit 1669f08

Please sign in to comment.