Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use an interface to construct Populator Pod Spec #171

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 41 additions & 31 deletions populator-machinery/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ type controller struct {
recorder record.EventRecorder
referenceGrantLister referenceGrantv1beta1.ReferenceGrantLister
referenceGrantSynced cache.InformerSynced
podConfig *PodConfig
podConfig PodSpecFactory
providerFunctionConfig *ProviderFunctionConfig
crossNamespace bool
}
Expand All @@ -133,7 +133,7 @@ type VolumePopulatorConfig struct {
Gvr schema.GroupVersionResource
// PodConfig is the configuration for creating populator pod. Either PodConfig or ProviderFunctionConfig should
// be specified. PodConfig and ProviderFunctionConfig can't be provided at the same time
PodConfig *PodConfig
PodConfig PodSpecFactory
// ProviderFunctionConfig is the configuration for invoking provider functions. Either PodConfig or ProviderFunctionConfig should
// be specified. PodConfig and ProviderFunctionConfig can't be provided at the same time
ProviderFunctionConfig *ProviderFunctionConfig
Expand All @@ -148,6 +148,10 @@ type VolumePopulatorConfig struct {
StopCh chan struct{}
}

type PodSpecFactory interface {
MakePopulatePodSpec(pvcPrimeName string, rawBlock bool, unstructured *unstructured.Unstructured) (*corev1.PodSpec, error)
}

type PodConfig struct {
// ImageName is the container image name
ImageName string
Expand Down Expand Up @@ -759,9 +763,7 @@ func (c *controller) syncPvc(ctx context.Context, key, pvcNamespace, pvcName str
rawBlock = true
}

// Calculate the args for the populator pod
var args []string
args, err = c.podConfig.PopulatorArgs(rawBlock, unstructured)
podSpec, err := c.podConfig.MakePopulatePodSpec(pvcPrimeName, rawBlock, unstructured)
if err != nil {
return err
}
Expand All @@ -772,26 +774,7 @@ func (c *controller) syncPvc(ctx context.Context, key, pvcNamespace, pvcName str
Name: podName,
Namespace: c.populatorNamespace,
},
Spec: makePopulatePodSpec(pvcPrimeName),
}
pod.Spec.Volumes[0].VolumeSource.PersistentVolumeClaim.ClaimName = pvcPrimeName
con := &pod.Spec.Containers[0]
con.Image = c.podConfig.ImageName
con.Args = args
if rawBlock {
con.VolumeDevices = []corev1.VolumeDevice{
{
Name: populatorPodVolumeName,
DevicePath: c.podConfig.DevicePath,
},
}
} else {
con.VolumeMounts = []corev1.VolumeMount{
{
Name: populatorPodVolumeName,
MountPath: c.podConfig.MountPath,
},
}
Spec: *podSpec,
}
if waitForFirstConsumer {
pod.Spec.NodeName = nodeName
Expand Down Expand Up @@ -925,13 +908,40 @@ func (c *controller) syncPvc(ctx context.Context, key, pvcNamespace, pvcName str
return nil
}

func makePopulatePodSpec(pvcPrimeName string) corev1.PodSpec {
return corev1.PodSpec{
Containers: []corev1.Container{
func (pc *PodConfig) MakePopulatePodSpec(pvcPrimeName string, rawBlock bool, unstructured *unstructured.Unstructured) (*corev1.PodSpec, error) {
// Calculate the args for the populator pod
var args []string
args, err := pc.PopulatorArgs(rawBlock, unstructured)
if err != nil {
return nil, err
}

con := corev1.Container{
Name: populatorContainerName,
ImagePullPolicy: corev1.PullIfNotPresent,
Image: pc.ImageName,
Args: args,
}

if rawBlock {
con.VolumeDevices = []corev1.VolumeDevice{
{
Name: populatorPodVolumeName,
DevicePath: pc.DevicePath,
},
}
} else {
con.VolumeMounts = []corev1.VolumeMount{
{
Name: populatorContainerName,
ImagePullPolicy: corev1.PullIfNotPresent,
Name: populatorPodVolumeName,
MountPath: pc.MountPath,
},
}
}

return &corev1.PodSpec{
Containers: []corev1.Container{
con,
},
RestartPolicy: corev1.RestartPolicyNever,
Volumes: []corev1.Volume{
Expand All @@ -944,7 +954,7 @@ func makePopulatePodSpec(pvcPrimeName string) corev1.PodSpec {
},
},
},
}
}, nil
}

func (c *controller) ensureFinalizer(ctx context.Context, pvc *corev1.PersistentVolumeClaim, finalizer string, want bool) error {
Expand Down