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

fix set dependent resources of the pediacluster lifecycle #287

Merged
merged 1 commit into from
Aug 3, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -326,51 +326,50 @@ func (c *Controller) reconcileLifecycle(lifecycle *policyv1alpha1.PediaClusterLi
return NoRequeueResult
}

references := make([]policyv1alpha1.DependentResource, 0, len(lifecycle.Spec.References))
referencesTemplateData := make(map[string]interface{}, len(lifecycle.Spec.References))
templateData := map[string]interface{}{
"source": sourceObj.UnstructuredContent(),
"references": referencesTemplateData,
}

// resolve and get reference resource
var writer bytes.Buffer
for _, ref := range lifecycle.Spec.References {
reference, err := ref.Resolve(&writer, templateData)
if err != nil {
condition.Reason = "FailedReferenceResourceParse"
condition.Message = fmt.Sprintf("failed to ressolve <%s> namespace and name: %v", ref, err)
if reason, message := func() (reason, message string) {
references := make([]policyv1alpha1.DependentResource, 0, len(lifecycle.Spec.References))
dependents := make(map[policyv1alpha1.DependentResource]struct{}, len(references)+1)
dependents[source] = struct{}{}
defer func() {
lifecycle.Status.References = references
c.dependentManager.SetLifecycleDependentResources(lifecycle.Name, dependents)
}()

klog.ErrorS(err, "failed to resolve reference namespacce and name", "lifecycle", lifecycle.Name, "reference", ref)
return NoRequeueResult
}
if reference.Name == "" {
condition.Reason = "FailedReferenceResourceParse"
condition.Message = fmt.Sprintf("<%s> resource name is empty", ref)
// resolve and get reference resource
for _, ref := range lifecycle.Spec.References {
reference, err := ref.Resolve(&writer, templateData)
if err != nil {
klog.ErrorS(err, "failed to resolve reference namespacce and name", "lifecycle", lifecycle.Name, "reference", ref)
return "FailedReferenceResourceParse", fmt.Sprintf("failed to ressolve <%s> namespace and name: %v", ref, err)
}
if reference.Name == "" {
klog.ErrorS(fmt.Errorf("reference resource name is empty"), "invalid reference resource", "lifecycle", lifecycle.Name, "reference", ref)
return "FailedReferenceResourceParse", fmt.Sprintf("<%s> resource name is empty", ref)
}

klog.ErrorS(fmt.Errorf("reference resource name is empty"), "invalid reference resource", "lifecycle", lifecycle.Name, "reference", ref)
return NoRequeueResult
}
dependents[reference] = struct{}{}
references = append(references, reference)

refObject, err := c.dependentManager.Get(reference)
if err != nil {
condition.Reason = "ReferenceResourceNotFound"
condition.Message = fmt.Sprintf("<%v>: %v", reference, err)
refObject, err := c.dependentManager.Get(reference)
if err != nil {
klog.ErrorS(err, "failed to get reference resource", "lifecycle", lifecycle.Name, "ref", ref)
return "ReferenceResourceNotFound", fmt.Sprintf("<%v>: %v", reference, err)
}

klog.ErrorS(err, "failed to get reference resource", "lifecycle", lifecycle.Name, "ref", ref)
return NoRequeueResult
referencesTemplateData[ref.Key] = refObject.UnstructuredContent()
}

referencesTemplateData[ref.Key] = refObject.UnstructuredContent()
references = append(references, reference)
}

referenceSet := make(map[policyv1alpha1.DependentResource]struct{}, len(references)+1)
referenceSet[source] = struct{}{}
for _, ref := range references {
referenceSet[ref] = struct{}{}
return
}(); reason != "" {
condition.Reason, condition.Message = reason, message
return NoRequeueResult
}
c.dependentManager.SetLifecycleDependentResources(lifecycle.Name, referenceSet)

if current == nil {
couldCreate, err := lifecycle.Spec.CouldCreate(&writer, templateData)
Expand Down Expand Up @@ -416,7 +415,6 @@ func (c *Controller) reconcileLifecycle(lifecycle *policyv1alpha1.PediaClusterLi
condition.Reason = "PediaClusterCreated"
condition.Message = ""
condition.Status = metav1.ConditionTrue
lifecycle.Status.References = references
klog.InfoS("pediacluster is created", "lifecycle", lifecycle.Name, "pediacluster", pediacluster.Name)
return NoRequeueResult
}
Expand Down Expand Up @@ -473,7 +471,6 @@ func (c *Controller) reconcileLifecycle(lifecycle *policyv1alpha1.PediaClusterLi
condition.Reason = "PediaClusterUpdated"
condition.Message = ""
condition.Status = metav1.ConditionTrue
lifecycle.Status.References = references
klog.InfoS("pediacluster is updated", "lifecycle", lifecycle.Name, "pediacluster", current.Name)
return NoRequeueResult
}
Expand Down