func (c *StashController) EnsureBackupTriggeringCronJob(invoker apis.Invoker) error {
	image := docker.Docker{
		Registry: c.DockerRegistry,
		Image:    docker.ImageStash,
		Tag:      c.StashImageTag,
	}

	meta := metav1.ObjectMeta{
		Name:      getBackupCronJobName(invoker.ObjectMeta.Name),
		Namespace: invoker.ObjectMeta.Namespace,
		Labels:    invoker.Labels,
	}

	// ensure respective ClusterRole,RoleBinding,ServiceAccount etc.
	var serviceAccountName string

	if invoker.RuntimeSettings.Pod != nil && invoker.RuntimeSettings.Pod.ServiceAccountName != "" {
		// ServiceAccount has been specified, so use it.
		serviceAccountName = invoker.RuntimeSettings.Pod.ServiceAccountName
	} else {
		// ServiceAccount hasn't been specified. so create new one with same name as BackupConfiguration object prefixed with stash-backup.
		serviceAccountName = meta.Name

		_, _, err := core_util.CreateOrPatchServiceAccount(c.kubeClient, meta, func(in *core.ServiceAccount) *core.ServiceAccount {
			core_util.EnsureOwnerReference(&in.ObjectMeta, invoker.OwnerRef)
			return in
		})
		if err != nil {
			return err
		}
	}

	// now ensure RBAC stuff for this CronJob
	err := stash_rbac.EnsureCronJobRBAC(c.kubeClient, invoker.OwnerRef, invoker.ObjectMeta.Namespace, serviceAccountName, c.getBackupSessionCronJobPSPNames(), invoker.Labels)
	if err != nil {
		return err
	}

	_, _, err = batch_util.CreateOrPatchCronJob(c.kubeClient, meta, func(in *batch_v1beta1.CronJob) *batch_v1beta1.CronJob {
		//set backup invoker object as cron-job owner
		core_util.EnsureOwnerReference(&in.ObjectMeta, invoker.OwnerRef)

		in.Spec.Schedule = invoker.Schedule
		in.Spec.Suspend = types.BoolP(invoker.Paused) // this ensure that the CronJob is suspended when the backup invoker is paused.
		in.Spec.JobTemplate.Labels = invoker.Labels
		// ensure that job gets deleted on completion
		in.Spec.JobTemplate.Labels[apis.KeyDeleteJobOnCompletion] = apis.AllowDeletingJobOnCompletion

		in.Spec.JobTemplate.Spec.Template.Spec.Containers = core_util.UpsertContainer(
			in.Spec.JobTemplate.Spec.Template.Spec.Containers,
			core.Container{
				Name:            apis.StashContainer,
				ImagePullPolicy: core.PullIfNotPresent,
				Image:           image.ToContainerImage(),
				Args: []string{
					"create-backupsession",
					fmt.Sprintf("--invoker-name=%s", invoker.OwnerRef.Name),
					fmt.Sprintf("--invoker-type=%s", invoker.OwnerRef.Kind),
				},
			})
		in.Spec.JobTemplate.Spec.Template.Spec.RestartPolicy = core.RestartPolicyNever
		in.Spec.JobTemplate.Spec.Template.Spec.ServiceAccountName = serviceAccountName
		// insert default pod level security context
		in.Spec.JobTemplate.Spec.Template.Spec.SecurityContext = util.UpsertDefaultPodSecurityContext(in.Spec.JobTemplate.Spec.Template.Spec.SecurityContext)
		return in
	})

	return err
}
// EnsureBackupTriggeringCronJob creates a Kubernetes CronJob for the respective backup invoker
// the CornJob will create a BackupSession object in each schedule
// respective BackupSession controller will watch this BackupSession object and take backup instantly
EnsureBackupTriggeringCronJob creates a Kubernetes CronJob for the respective backup invoker
the CornJob will create a BackupSession object in each schedule
respective BackupSession controller will watch this BackupSession object and take backup instantly
// EnsureBackupTriggeringCronJob creates a Kubernetes CronJob for the respective backup invoker // the CornJob will create a BackupSession object in each schedule // respective BackupSession controller will watch this BackupSession object and take backup instantly
func (c *StashController) EnsureBackupTriggeringCronJob(invoker apis.Invoker) error {
	image := docker.Docker{
		Registry: c.DockerRegistry,
		Image:    docker.ImageStash,
		Tag:      c.StashImageTag,
	}
	meta := metav1.ObjectMeta{
		Name:      getBackupCronJobName(invoker.ObjectMeta.Name),
		Namespace: invoker.ObjectMeta.Namespace,
		Labels:    invoker.Labels,
	}
	var serviceAccountName string
	if invoker.RuntimeSettings.Pod != nil && invoker.RuntimeSettings.Pod.ServiceAccountName != "" {
		serviceAccountName = invoker.RuntimeSettings.Pod.ServiceAccountName
	} else {
		serviceAccountName = meta.Name
		_, _, err := core_util.CreateOrPatchServiceAccount(c.kubeClient, meta, func(in *core.ServiceAccount) *core.ServiceAccount {
			core_util.EnsureOwnerReference(&in.ObjectMeta, invoker.OwnerRef)
			return in
		})
		if err != nil {
			return err
		}
	}
	err := stash_rbac.EnsureCronJobRBAC(c.kubeClient, invoker.OwnerRef, invoker.ObjectMeta.Namespace, serviceAccountName, c.getBackupSessionCronJobPSPNames(), invoker.Labels)
	if err != nil {
		return err
	}
	_, _, err = batch_util.CreateOrPatchCronJob(c.kubeClient, meta, func(in *batch_v1beta1.CronJob) *batch_v1beta1.CronJob {
		core_util.EnsureOwnerReference(&in.ObjectMeta, invoker.OwnerRef)
		in.Spec.Schedule = invoker.Schedule
		in.Spec.Suspend = types.BoolP(invoker.Paused) 
		in.Spec.JobTemplate.Labels = invoker.Labels
		in.Spec.JobTemplate.Labels[apis.KeyDeleteJobOnCompletion] = apis.AllowDeletingJobOnCompletion
		in.Spec.JobTemplate.Spec.Template.Spec.Containers = core_util.UpsertContainer(
			in.Spec.JobTemplate.Spec.Template.Spec.Containers,
			core.Container{
				Name:            apis.StashContainer,
				ImagePullPolicy: core.PullIfNotPresent,
				Image:           image.ToContainerImage(),
				Args: []string{
					"create-backupsession",
					fmt.Sprintf("--invoker-name=%s", invoker.OwnerRef.Name),
					fmt.Sprintf("--invoker-type=%s", invoker.OwnerRef.Kind),
				},
			})
		in.Spec.JobTemplate.Spec.Template.Spec.RestartPolicy = core.RestartPolicyNever
		in.Spec.JobTemplate.Spec.Template.Spec.ServiceAccountName = serviceAccountName
		in.Spec.JobTemplate.Spec.Template.Spec.SecurityContext = util.UpsertDefaultPodSecurityContext(in.Spec.JobTemplate.Spec.Template.Spec.SecurityContext)
		return in
	})
	return err
}
func ( c * StashController ) EnsureBackupTriggeringCronJob ( invoker apis . Invoker ) error { image := docker . Docker { Registry : c . DockerRegistry , Image : docker . ImageStash , Tag : c . StashImageTag , } meta := metav1 . ObjectMeta { Name : getBackupCronJobName ( invoker . ObjectMeta . Name ) , Namespace : invoker . ObjectMeta . Namespace , Labels : invoker . Labels , } var serviceAccountName string if invoker . RuntimeSettings . Pod != nil && invoker . RuntimeSettings . Pod . ServiceAccountName != " " { serviceAccountName = invoker . RuntimeSettings . Pod . ServiceAccountName } else { serviceAccountName = meta . Name _ , _ , err := core_util . CreateOrPatchServiceAccount ( c . kubeClient , meta , func ( in * core . ServiceAccount ) * core . ServiceAccount { core_util . EnsureOwnerReference ( & in . ObjectMeta , invoker . OwnerRef ) return in } ) if err != nil { return err } } err := stash_rbac . EnsureCronJobRBAC ( c . kubeClient , invoker . OwnerRef , invoker . ObjectMeta . Namespace , serviceAccountName , c . getBackupSessionCronJobPSPNames ( ) , invoker . Labels ) if err != nil { return err } _ , _ , err = batch_util . CreateOrPatchCronJob ( c . kubeClient , meta , func ( in * batch_v1beta1 . CronJob ) * batch_v1beta1 . CronJob { core_util . EnsureOwnerReference ( & in . ObjectMeta , invoker . OwnerRef ) in . Spec . Schedule = invoker . Schedule in . Spec . Suspend = types . BoolP ( invoker . Paused ) in . Spec . JobTemplate . Labels = invoker . Labels in . Spec . JobTemplate . Labels [ apis . KeyDeleteJobOnCompletion ] = apis . AllowDeletingJobOnCompletion in . Spec . JobTemplate . Spec . Template . Spec . Containers = core_util . UpsertContainer ( in . Spec . JobTemplate . Spec . Template . Spec . Containers , core . Container { Name : apis . StashContainer , ImagePullPolicy : core . PullIfNotPresent , Image : image . ToContainerImage ( ) , Args : [ ] string { " " , fmt . Sprintf ( " " , invoker . OwnerRef . Name ) , fmt . Sprintf ( " " , invoker . OwnerRef . Kind ) , } , } ) in . Spec . JobTemplate . Spec . Template . Spec . RestartPolicy = core . RestartPolicyNever in . Spec . JobTemplate . Spec . Template . Spec . ServiceAccountName = serviceAccountName in . Spec . JobTemplate . Spec . Template . Spec . SecurityContext = util . UpsertDefaultPodSecurityContext ( in . Spec . JobTemplate . Spec . Template . Spec . SecurityContext ) return in } ) return err }
EnsureBackupTriggeringCronJob creates a Kubernetes CronJob for the respective backup invoker
the CornJob will create a BackupSession object in each schedule
respective BackupSession controller will watch this BackupSession object and take backup instantly
EnsureBackupTriggeringCronJob creates a Kubernetes CronJob for the respective backup invoker the CornJob will create a BackupSession object in each schedule respective BackupSession controller will watch this BackupSession object and take backup instantly
// ensure respective ClusterRole,RoleBinding,ServiceAccount etc. // ServiceAccount has been specified, so use it. // ServiceAccount hasn't been specified. so create new one with same name as BackupConfiguration object prefixed with stash-backup. // now ensure RBAC stuff for this CronJob //set backup invoker object as cron-job owner // this ensure that the CronJob is suspended when the backup invoker is paused. // ensure that job gets deleted on completion // insert default pod level security context