(desiredCrd *apiextv1.CustomResourceDefinition)
| 54 | } |
| 55 | |
| 56 | func (c *Controller) createOperatorCRD(desiredCrd *apiextv1.CustomResourceDefinition) error { |
| 57 | crd, err := c.KubeClient.CustomResourceDefinitions().Get(context.TODO(), desiredCrd.Name, metav1.GetOptions{}) |
| 58 | if k8sutil.ResourceNotFound(err) { |
| 59 | if _, err := c.KubeClient.CustomResourceDefinitions().Create(context.TODO(), desiredCrd, metav1.CreateOptions{}); err != nil { |
| 60 | return fmt.Errorf("could not create customResourceDefinition %q: %v", desiredCrd.Name, err) |
| 61 | } |
| 62 | } |
| 63 | if err != nil { |
| 64 | c.logger.Errorf("could not get customResourceDefinition %q: %v", desiredCrd.Name, err) |
| 65 | } |
| 66 | if crd != nil { |
| 67 | c.logger.Infof("customResourceDefinition %q is already registered and will only be updated", crd.Name) |
| 68 | // copy annotations and labels from existing CRD since we do not define them |
| 69 | desiredCrd.Annotations = crd.Annotations |
| 70 | desiredCrd.Labels = crd.Labels |
| 71 | patch, err := json.Marshal(desiredCrd) |
| 72 | if err != nil { |
| 73 | return fmt.Errorf("could not marshal new customResourceDefintion %q: %v", desiredCrd.Name, err) |
| 74 | } |
| 75 | if _, err := c.KubeClient.CustomResourceDefinitions().Patch( |
| 76 | context.TODO(), crd.Name, types.MergePatchType, patch, metav1.PatchOptions{}); err != nil { |
| 77 | return fmt.Errorf("could not update customResourceDefinition %q: %v", crd.Name, err) |
| 78 | } |
| 79 | } |
| 80 | c.logger.Infof("customResourceDefinition %q is registered", crd.Name) |
| 81 | |
| 82 | return wait.PollUntilContextTimeout(context.TODO(), c.config.CRDReadyWaitInterval, c.config.CRDReadyWaitTimeout, false, func(ctx context.Context) (bool, error) { |
| 83 | c, err := c.KubeClient.CustomResourceDefinitions().Get(context.TODO(), desiredCrd.Name, metav1.GetOptions{}) |
| 84 | if err != nil { |
| 85 | return false, err |
| 86 | } |
| 87 | |
| 88 | for _, cond := range c.Status.Conditions { |
| 89 | switch cond.Type { |
| 90 | case apiextv1.Established: |
| 91 | if cond.Status == apiextv1.ConditionTrue { |
| 92 | return true, err |
| 93 | } |
| 94 | case apiextv1.NamesAccepted: |
| 95 | if cond.Status == apiextv1.ConditionFalse { |
| 96 | return false, fmt.Errorf("name conflict: %v", cond.Reason) |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | return false, err |
| 102 | }) |
| 103 | } |
| 104 | |
| 105 | func (c *Controller) createPostgresCRD() error { |
| 106 | return c.createOperatorCRD(acidv1.PostgresCRD(c.opConfig.CRDCategories)) |
no test coverage detected