rename function name that ambiguous (#54)

This commit is contained in:
Xianpao Chen 2020-12-07 10:02:52 +08:00 committed by GitHub
parent c94a795299
commit 61c86f7a0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 30 deletions

View File

@ -61,7 +61,7 @@ func (c *PropagationBindingController) Reconcile(req controllerruntime.Request)
func (c *PropagationBindingController) syncBinding(binding *v1alpha1.PropagationBinding) (controllerruntime.Result, error) {
err := c.transformBindingToWorks(binding)
if err != nil {
klog.Errorf("failed to transform propagationBinding %s/%s to propagationWorks. error: %+v",
klog.Errorf("Failed to transform propagationBinding %s/%s to propagationWorks. Error: %+v",
binding.Namespace, binding.Name, err)
return controllerruntime.Result{Requeue: true}, err
}
@ -88,16 +88,18 @@ func (c *PropagationBindingController) removeIrrelevantField(workload *unstructu
unstructured.RemoveNestedField(workload.Object, "metadata", "generation")
unstructured.RemoveNestedField(workload.Object, "metadata", "resourceVersion")
unstructured.RemoveNestedField(workload.Object, "metadata", "selfLink")
unstructured.RemoveNestedField(workload.Object, "metadata", "managedFields")
unstructured.RemoveNestedField(workload.Object, "metadata", "uid")
unstructured.RemoveNestedField(workload.Object, "status")
}
// transformBindingToWorks will transform propagationBinding to propagationWorks
func (c *PropagationBindingController) transformBindingToWorks(binding *v1alpha1.PropagationBinding) error {
workload, err := util.GetResourceStructure(c.DynamicClient, binding.Spec.Resource.APIVersion,
workload, err := util.GetUnstructured(c.DynamicClient, binding.Spec.Resource.APIVersion,
binding.Spec.Resource.Kind, binding.Spec.Resource.Namespace, binding.Spec.Resource.Name)
if err != nil {
klog.Errorf("failed to get resource. error: %v", err)
klog.Errorf("Failed to get resource, kind: %s, namespace: %s, name: %s. Error: %v",
binding.Spec.Resource.Kind, binding.Spec.Resource.Namespace, binding.Spec.Resource.Name, err)
return err
}
@ -116,7 +118,8 @@ func (c *PropagationBindingController) ensurePropagationWork(workload *unstructu
c.removeIrrelevantField(workload)
formatWorkload, err := workload.MarshalJSON()
if err != nil {
klog.Errorf("failed to marshal workload. error: %v", err)
klog.Errorf("Failed to marshal workload, kind: %s, namespace: %s, name: %s. Error: %v",
workload.GetKind(), workload.GetName(), workload.GetNamespace(), err)
return err
}
rawExtension := runtime.RawExtension{
@ -145,23 +148,23 @@ func (c *PropagationBindingController) ensurePropagationWork(workload *unstructu
if err != nil && apierrors.IsNotFound(err) {
_, err := c.KarmadaClient.PropagationstrategyV1alpha1().PropagationWorks(clusterNameMirrorNamespace).Create(context.TODO(), &propagationWork, metav1.CreateOptions{})
if err != nil {
klog.Errorf("failed to create propagationWork %s/%s. error: %v", clusterNameMirrorNamespace, propagationWork.Name, err)
klog.Errorf("Failed to create propagationWork %s/%s. Error: %v", clusterNameMirrorNamespace, propagationWork.Name, err)
return err
}
klog.Infof("create propagationWork %s/%s success", clusterNameMirrorNamespace, propagationWork.Name)
klog.Infof("Create propagationWork %s/%s successfully", clusterNameMirrorNamespace, propagationWork.Name)
continue
} else if err != nil && !apierrors.IsNotFound(err) {
klog.Errorf("failed to get propagationWork %s/%s. error: %v", clusterNameMirrorNamespace, propagationWork.Name, err)
klog.Errorf("Failed to get propagationWork %s/%s. Error: %v", clusterNameMirrorNamespace, propagationWork.Name, err)
return err
}
workGetResult.Spec = propagationWork.Spec
workGetResult.ObjectMeta.OwnerReferences = propagationWork.ObjectMeta.OwnerReferences
_, err = c.KarmadaClient.PropagationstrategyV1alpha1().PropagationWorks(clusterNameMirrorNamespace).Update(context.TODO(), workGetResult, metav1.UpdateOptions{})
if err != nil {
klog.Errorf("failed to update propagationWork %s/%s. error: %v", clusterNameMirrorNamespace, propagationWork.Name, err)
klog.Errorf("Failed to update propagationWork %s/%s. Error: %v", clusterNameMirrorNamespace, propagationWork.Name, err)
return err
}
klog.Infof("update propagationWork %s/%s success", clusterNameMirrorNamespace, propagationWork.Name)
klog.Infof("Update propagationWork %s/%s successfully", clusterNameMirrorNamespace, propagationWork.Name)
}
return nil
}

View File

@ -84,19 +84,19 @@ func (c *PropagationPolicyController) fetchWorkloads(resourceSelectors []v1alpha
// todo: if resources repetitive, deduplication.
// todo: if namespaces, names, labelSelector is nil, need to do something
for _, resourceSelector := range resourceSelectors {
matchNamespaces := util.GetDifferenceSet(resourceSelector.Namespaces, resourceSelector.ExcludeNamespaces)
deduplicationNames := util.GetUniqueElements(resourceSelector.Names)
for _, namespace := range matchNamespaces {
names := util.GetUniqueElements(resourceSelector.Names)
namespaces := util.GetDifferenceSet(resourceSelector.Namespaces, resourceSelector.ExcludeNamespaces)
for _, namespace := range namespaces {
if resourceSelector.LabelSelector == nil {
err := c.fetchWorkloadsWithOutLabelSelector(resourceSelector, namespace, deduplicationNames, &workloads)
err := c.fetchWorkloadsWithOutLabelSelector(resourceSelector, namespace, names, &workloads)
if err != nil {
klog.Errorf("failed to fetch workloads by names in namespace %s. error: %v", namespace, err)
klog.Errorf("Failed to fetch workloads by names in namespace %s. Error: %v", namespace, err)
return nil, err
}
} else {
err := c.fetchWorkloadsWithLabelSelector(resourceSelector, namespace, deduplicationNames, &workloads)
err := c.fetchWorkloadsWithLabelSelector(resourceSelector, namespace, names, &workloads)
if err != nil {
klog.Errorf("failed to fetch workloads with labelSelector in namespace %s. error: %v", namespace, err)
klog.Errorf("Failed to fetch workloads with labelSelector in namespace %s. Error: %v", namespace, err)
return nil, err
}
}
@ -121,7 +121,7 @@ func (c *PropagationPolicyController) buildPropagationBinding(policy *v1alpha1.P
// fetchWorkloadsWithLabelSelector query workloads by labelSelector and names
func (c *PropagationPolicyController) fetchWorkloadsWithLabelSelector(resourceSelector v1alpha1.ResourceSelector, namespace string, names []string, workloads *[]*unstructured.Unstructured) error {
unstructuredWorkLoadList, err := util.GetResourcesStructureByFilter(c.DynamicClient, resourceSelector.APIVersion,
unstructuredWorkLoadList, err := util.ListUnstructuredByFilter(c.DynamicClient, resourceSelector.APIVersion,
resourceSelector.Kind, namespace, resourceSelector.LabelSelector)
if err != nil {
return err
@ -146,7 +146,7 @@ func (c *PropagationPolicyController) fetchWorkloadsWithLabelSelector(resourceSe
// fetchWorkloadsWithOutLabelSelector query workloads by names
func (c *PropagationPolicyController) fetchWorkloadsWithOutLabelSelector(resourceSelector v1alpha1.ResourceSelector, namespace string, names []string, workloads *[]*unstructured.Unstructured) error {
for _, name := range names {
workload, err := util.GetResourceStructure(c.DynamicClient, resourceSelector.APIVersion,
workload, err := util.GetUnstructured(c.DynamicClient, resourceSelector.APIVersion,
resourceSelector.Kind, namespace, name)
if err != nil {
return err
@ -162,7 +162,7 @@ func (c *PropagationPolicyController) getTargetClusters(placement v1alpha1.Place
matchClusterNames := util.GetDifferenceSet(placement.ClusterAffinity.ClusterNames, placement.ClusterAffinity.ExcludeClusters)
// todo: cluster labelSelector, fieldSelector, clusterTolerations
// todo: calc spread contraints. such as maximumClusters, minimumClusters
// todo: calc spread contraints. such as maximum, minimum
var targetClusters []v1alpha1.TargetCluster
for _, matchClusterName := range matchClusterNames {
targetClusters = append(targetClusters, v1alpha1.TargetCluster{Name: matchClusterName})
@ -197,23 +197,23 @@ func (c *PropagationPolicyController) ensurePropagationBinding(propagationPolicy
if err != nil && apierrors.IsNotFound(err) {
_, err := c.KarmadaClient.PropagationstrategyV1alpha1().PropagationBindings(propagationBinding.Namespace).Create(context.TODO(), &propagationBinding, metav1.CreateOptions{})
if err != nil {
klog.Errorf("failed to create propagationBinding %s/%s. error: %v", propagationBinding.Namespace, propagationBinding.Name, err)
klog.Errorf("Failed to create propagationBinding %s/%s. Error: %v", propagationBinding.Namespace, propagationBinding.Name, err)
return err
}
klog.Infof("create propagationBinding %s/%s success", propagationBinding.Namespace, propagationBinding.Name)
klog.Infof("Create propagationBinding %s/%s successfully", propagationBinding.Namespace, propagationBinding.Name)
return nil
} else if err != nil && !apierrors.IsNotFound(err) {
klog.Errorf("failed to get propagationBinding %s/%s. error: %v", propagationBinding.Namespace, propagationBinding.Name, err)
klog.Errorf("Failed to get propagationBinding %s/%s. Error: %v", propagationBinding.Namespace, propagationBinding.Name, err)
return err
}
bindingGetResult.Spec = propagationBinding.Spec
bindingGetResult.ObjectMeta.OwnerReferences = propagationBinding.ObjectMeta.OwnerReferences
_, err = c.KarmadaClient.PropagationstrategyV1alpha1().PropagationBindings(propagationBinding.Namespace).Update(context.TODO(), bindingGetResult, metav1.UpdateOptions{})
if err != nil {
klog.Errorf("failed to update propagationBinding %s/%s. error: %v", propagationBinding.Namespace, propagationBinding.Name, err)
klog.Errorf("Failed to update propagationBinding %s/%s. Error: %v", propagationBinding.Namespace, propagationBinding.Name, err)
return err
}
klog.Infof("update propagationBinding %s/%s success", propagationBinding.Namespace, propagationBinding.Name)
klog.Infof("Update propagationBinding %s/%s successfully", propagationBinding.Namespace, propagationBinding.Name)
return nil
}

View File

@ -41,8 +41,8 @@ func generateGroupVersionResource(apiVersion, kind string) (schema.GroupVersionR
return dynamicResource, nil
}
// GetResourceStructure get resource yaml from kubernetes
func GetResourceStructure(client dynamic.Interface, apiVersion, kind, namespace, name string) (*unstructured.Unstructured, error) {
// GetUnstructured will get unstructured object
func GetUnstructured(client dynamic.Interface, apiVersion, kind, namespace, name string) (*unstructured.Unstructured, error) {
dynamicResource, err := generateGroupVersionResource(apiVersion, kind)
if err != nil {
return nil, err
@ -54,8 +54,8 @@ func GetResourceStructure(client dynamic.Interface, apiVersion, kind, namespace,
return result, nil
}
// GetResourcesStructureByFilter get resources yaml from kubernetes by filter
func GetResourcesStructureByFilter(client dynamic.Interface, apiVersion, kind, namespace string, labelSelector *metav1.LabelSelector) (*unstructured.UnstructuredList, error) {
// ListUnstructuredByFilter will list unstructuredList by labelSelector
func ListUnstructuredByFilter(client dynamic.Interface, apiVersion, kind, namespace string, labelSelector *metav1.LabelSelector) (*unstructured.UnstructuredList, error) {
dynamicResource, err := generateGroupVersionResource(apiVersion, kind)
if err != nil {
return nil, err
@ -68,7 +68,7 @@ func GetResourcesStructureByFilter(client dynamic.Interface, apiVersion, kind, n
return result, nil
}
// GetDifferenceSet get match item by compare include items and exclude items
// GetDifferenceSet will get difference set from includeItems and excludeItems
func GetDifferenceSet(includeItems, excludeItems []string) []string {
if includeItems == nil {
includeItems = []string{}
@ -90,7 +90,7 @@ func GetDifferenceSet(includeItems, excludeItems []string) []string {
return matchItems.List()
}
// GetUniqueElements get deduplication array
// GetUniqueElements will delete duplicate element in list
func GetUniqueElements(list []string) []string {
if list == nil {
return []string{}