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

issue-545, remove self composing id for the opensearch egress rules #615

Merged
Show file tree
Hide file tree
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 @@ -120,7 +120,7 @@ func (r *OpenSearchEgressRulesReconciler) handleCreate(ctx context.Context, l lo
}

if errors.Is(err, instaclustr.NotFound) {
err = r.API.CreateOpenSearchEgressRules(rule)
rule.Status.ID, err = r.API.CreateOpenSearchEgressRules(rule)
if err != nil {
l.Error(err, "failed to create OpenSearch Egress Rule resource on Instaclustr")
r.EventRecorder.Eventf(rule, models.Warning, models.CreationFailed,
Expand All @@ -129,6 +129,7 @@ func (r *OpenSearchEgressRulesReconciler) handleCreate(ctx context.Context, l lo

return err
}

}

err = r.Status().Patch(ctx, rule, patch)
Expand Down Expand Up @@ -161,10 +162,6 @@ func (r *OpenSearchEgressRulesReconciler) handleCreate(ctx context.Context, l lo
}

func (r *OpenSearchEgressRulesReconciler) handleDelete(ctx context.Context, logger logr.Logger, rule *clusterresourcesv1beta1.OpenSearchEgressRules) error {
if rule.Status.ID == "" {
rule.Status.ID = fmt.Sprintf("%s~%s~%s", rule.Spec.ClusterID, rule.Spec.Source, rule.Spec.OpenSearchBindingID)
}

_, err := r.API.GetOpenSearchEgressRule(rule.Status.ID)
if !errors.Is(err, instaclustr.NotFound) && err != nil {
logger.Error(err, "failed to get OpenSearch Egress Rule resource from Instaclustr")
Expand Down
21 changes: 11 additions & 10 deletions pkg/instaclustr/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2325,39 +2325,40 @@ func (c *Client) GetAWSVPCPeering(peerID string) (*models.AWSVPCPeering, error)
return &vpcPeering, nil
}

func (c *Client) CreateOpenSearchEgressRules(rule *clusterresourcesv1beta1.OpenSearchEgressRules) error {
func (c *Client) CreateOpenSearchEgressRules(rule *clusterresourcesv1beta1.OpenSearchEgressRules) (string, error) {
url := c.serverHostname + OpenSearchEgressRulesEndpoint
status := &clusterresourcesv1beta1.OpenSearchEgressRulesStatus{}

b, err := json.Marshal(rule.Spec)
if err != nil {
return err
return "", err
}

resp, err := c.DoRequest(url, http.MethodPost, b)
if err != nil {
return err
return "", err
}

defer resp.Body.Close()
b, err = io.ReadAll(resp.Body)
if err != nil {
return err
return "", err
}

if resp.StatusCode != http.StatusAccepted {
return fmt.Errorf("status code: %d, message: %s", resp.StatusCode, b)
return "", fmt.Errorf("status code: %d, message: %s", resp.StatusCode, b)
}

err = json.Unmarshal(b, &rule.Status)
err = json.Unmarshal(b, status)
if err != nil {
return err
return "", err
}

return nil
return status.ID, nil
}

func (c *Client) GetOpenSearchEgressRule(id string) (*clusterresourcesv1beta1.OpenSearchEgressRules, error) {
rule := clusterresourcesv1beta1.OpenSearchEgressRules{}
func (c *Client) GetOpenSearchEgressRule(id string) (*clusterresourcesv1beta1.OpenSearchEgressRulesStatus, error) {
rule := clusterresourcesv1beta1.OpenSearchEgressRulesStatus{}
url := c.serverHostname + OpenSearchEgressRulesEndpoint + "/" + id

resp, err := c.DoRequest(url, http.MethodGet, nil)
Expand Down
4 changes: 2 additions & 2 deletions pkg/instaclustr/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ type API interface {
GetDefaultCredentialsV1(clusterID string) (string, string, error)
UpdateClusterSettings(clusterID string, settings *models.ClusterSettings) error
GetAWSEndpointServicePrincipal(id string) (*models.AWSEndpointServicePrincipal, error)
CreateOpenSearchEgressRules(rule *clusterresourcesv1beta1.OpenSearchEgressRules) error
GetOpenSearchEgressRule(id string) (*clusterresourcesv1beta1.OpenSearchEgressRules, error)
CreateOpenSearchEgressRules(rule *clusterresourcesv1beta1.OpenSearchEgressRules) (string, error)
GetOpenSearchEgressRule(id string) (*clusterresourcesv1beta1.OpenSearchEgressRulesStatus, error)
DeleteOpenSearchEgressRule(id string) error
CreateAWSEndpointServicePrincipal(spec any) ([]byte, error)
DeleteAWSEndpointServicePrincipal(principalID string) error
Expand Down
4 changes: 2 additions & 2 deletions pkg/instaclustr/mock/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,11 +414,11 @@ func (c *mockClient) GetAWSEndpointServicePrincipal(id string) (*models.AWSEndpo
panic("GetAWSEndpointServicePrincipal: is not implemented")
}

func (c *mockClient) CreateOpenSearchEgressRules(rule *clusterresourcesv1beta1.OpenSearchEgressRules) error {
func (c *mockClient) CreateOpenSearchEgressRules(rule *clusterresourcesv1beta1.OpenSearchEgressRules) (string, error) {
panic("CreateOpenSearchEgressRules: is not implemented")
}

func (c *mockClient) GetOpenSearchEgressRule(id string) (*clusterresourcesv1beta1.OpenSearchEgressRules, error) {
func (c *mockClient) GetOpenSearchEgressRule(id string) (*clusterresourcesv1beta1.OpenSearchEgressRulesStatus, error) {
panic("GetOpenSearchEgressRule: is not implemented")
}

Expand Down