Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
rkthtrifork committed Jun 3, 2024
1 parent 4439b63 commit 45f36ed
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"net/http"
"strings"

"github.com/go-logr/logr"
"github.com/opensearch-project/opensearch-go"
"github.com/opensearch-project/opensearch-go/opensearchapi"
)
Expand Down Expand Up @@ -50,7 +49,7 @@ func doHTTPHead(ctx context.Context, client *opensearch.Client, path strings.Bui
}

// doHTTPPut performs a HTTP PUT request
func doHTTPPut(ctx context.Context, client *opensearch.Client, path strings.Builder, body io.Reader, logger *logr.Logger) (*opensearchapi.Response, error) {
func doHTTPPut(ctx context.Context, client *opensearch.Client, path strings.Builder, body io.Reader) (*opensearchapi.Response, error) {
req, err := http.NewRequest(http.MethodPut, path.String(), body)
if err != nil {
return nil, err
Expand Down
19 changes: 6 additions & 13 deletions opensearch-operator/opensearch-gateway/services/os_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ import (
"time"

"github.com/Opster/opensearch-k8s-operator/opensearch-operator/opensearch-gateway/responses"
"github.com/go-logr/logr"
"github.com/opensearch-project/opensearch-go"
"github.com/opensearch-project/opensearch-go/opensearchapi"
"github.com/opensearch-project/opensearch-go/opensearchutil"
"k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/log"
)

const (
Expand Down Expand Up @@ -301,8 +299,7 @@ func (client *OsClusterClient) GetSecurityResource(ctx context.Context, resource
// PutSecurityResource performs an HTTP PUT request to OS to create/update the security resource specified by name
func (client *OsClusterClient) PutSecurityResource(ctx context.Context, resource, name string, body io.Reader) (*opensearchapi.Response, error) {
path := generateAPIPath(resource, name)
logger := log.FromContext(ctx).WithValues("reconciler", "ismpolicy")
return doHTTPPut(ctx, client.client, path, body, &logger)
return doHTTPPut(ctx, client.client, path, body)
}

// DeleteSecurityResource performs an HTTP DELETE request to OS to delete the security resource specified by name
Expand All @@ -318,17 +315,15 @@ func (client *OsClusterClient) GetISMConfig(ctx context.Context, name string) (*
}

// PutISMConfig performs an HTTP PUT request to OS to create the ISM policy resource specified by name
func (client *OsClusterClient) PutISMConfig(ctx context.Context, name string, body io.Reader, logger *logr.Logger) (*opensearchapi.Response, error) {
func (client *OsClusterClient) PutISMConfig(ctx context.Context, name string, body io.Reader) (*opensearchapi.Response, error) {
path := generateAPIPathISM(ismResource, name)
logger.Info("rkth path: " + path.String())
return doHTTPPut(ctx, client.client, path, body, logger)
return doHTTPPut(ctx, client.client, path, body)
}

// UpdateISMConfig performs an HTTP PUT request to OS to update the ISM policy resource specified by name
func (client *OsClusterClient) UpdateISMConfig(ctx context.Context, name string, seqnumber, primterm int, body io.Reader) (*opensearchapi.Response, error) {
path := generateAPIPathUpdateISM(ismResource, name, seqnumber, primterm)
logger := log.FromContext(ctx).WithValues("reconciler", "ismpolicy")
return doHTTPPut(ctx, client.client, path, body, &logger)
return doHTTPPut(ctx, client.client, path, body)
}

// DeleteISMConfig performs an HTTP DELETE request to OS to delete the ISM policy resource specified by name
Expand All @@ -346,15 +341,13 @@ func (client *OsClusterClient) GetSnapshotRepository(ctx context.Context, name s
// performs an HTTP PUT request to OS to create the snapshot repository specified by name
func (client *OsClusterClient) CreateSnapshotRepository(ctx context.Context, name string, body io.Reader) (*opensearchapi.Response, error) {
path := generateAPIPathSnapshotRepository(name)
logger := log.FromContext(ctx).WithValues("reconciler", "ismpolicy")
return doHTTPPut(ctx, client.client, path, body, &logger)
return doHTTPPut(ctx, client.client, path, body)
}

// performs an HTTP PUT request to OS to update the snapshot repository specified by name
func (client *OsClusterClient) UpdateSnapshotRepository(ctx context.Context, name string, body io.Reader) (*opensearchapi.Response, error) {
path := generateAPIPathSnapshotRepository(name)
logger := log.FromContext(ctx).WithValues("reconciler", "ismpolicy")
return doHTTPPut(ctx, client.client, path, body, &logger)
return doHTTPPut(ctx, client.client, path, body)
}

// DeleteISMConfig performs an HTTP DELETE request to OS to delete the ISM policy resource specified by name
Expand Down
14 changes: 4 additions & 10 deletions opensearch-operator/opensearch-gateway/services/os_data_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,7 @@ func AppendExcludeNodeHost(service *OsClusterClient, nodeNameToExclude string) (
valAsString = strings.Join(valArr, ",")
}
settings := createClusterSettingsResponseWithExcludeName(valAsString)
if err == nil {
_, err = service.PutClusterSettings(settings)
}
_, err = service.PutClusterSettings(settings)
return err == nil, err
}

Expand All @@ -115,9 +113,7 @@ func RemoveExcludeNodeHost(service *OsClusterClient, nodeNameToExclude string) (
valAsString := strings.ReplaceAll(val.(string), nodeNameToExclude, "")
valAsString = strings.ReplaceAll(valAsString, ",,", ",")
settings := createClusterSettingsResponseWithExcludeName(valAsString)
if err == nil {
_, err = service.PutClusterSettings(settings)
}
_, err = service.PutClusterSettings(settings)
return err == nil, err
}

Expand Down Expand Up @@ -385,8 +381,7 @@ func CreateOrUpdateIndexTemplate(
) error {
path := IndexTemplatePath(indexTemplateName)

logger := log.FromContext(ctx).WithValues("reconciler", "ismpolicy")
resp, err := doHTTPPut(ctx, service.client, path, opensearchutil.NewJSONReader(indexTemplate), &logger)
resp, err := doHTTPPut(ctx, service.client, path, opensearchutil.NewJSONReader(indexTemplate))
if err != nil {
return err
}
Expand Down Expand Up @@ -511,8 +506,7 @@ func CreateOrUpdateComponentTemplate(
) error {
path := ComponentTemplatePath(componentTemplateName)

logger := log.FromContext(ctx).WithValues("reconciler", "ismpolicy")
resp, err := doHTTPPut(ctx, service.client, path, opensearchutil.NewJSONReader(componentTemplate), &logger)
resp, err := doHTTPPut(ctx, service.client, path, opensearchutil.NewJSONReader(componentTemplate))
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"

"github.com/Opster/opensearch-k8s-operator/opensearch-operator/opensearch-gateway/requests"
"github.com/go-logr/logr"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/opensearch-project/opensearch-go/opensearchutil"
Expand Down Expand Up @@ -67,17 +66,14 @@ func GetPolicy(ctx context.Context, service *OsClusterClient, policyName string)
}

// CreateISMPolicy creates the passed policy
func CreateISMPolicy(ctx context.Context, service *OsClusterClient, ismpolicy requests.Policy, policyId string, logger *logr.Logger) error {
func CreateISMPolicy(ctx context.Context, service *OsClusterClient, ismpolicy requests.Policy, policyId string) error {
spec := opensearchutil.NewJSONReader(ismpolicy)
resp, err := service.PutISMConfig(ctx, policyId, spec, logger)
logger.Info("rkth hej1")
resp, err := service.PutISMConfig(ctx, policyId, spec)
if err != nil {
logger.Info("rkth hej2")
return err
}
defer resp.Body.Close()
if resp.IsError() {
logger.Info("rkth hej3")
return fmt.Errorf("failed to create ism policy: %s", resp.String())
}
return nil
Expand Down
13 changes: 1 addition & 12 deletions opensearch-operator/pkg/reconcilers/ismpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/Opster/opensearch-k8s-operator/opensearch-operator/pkg/reconcilers/k8s"
"github.com/Opster/opensearch-k8s-operator/opensearch-operator/pkg/reconcilers/util"
"github.com/cisco-open/operator-tools/pkg/reconciler"
"github.com/davecgh/go-spew/spew"
"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
Expand Down Expand Up @@ -210,19 +209,9 @@ func (r *IsmPolicyReconciler) Reconcile() (retResult ctrl.Result, retErr error)
}
if errors.Is(retErr, services.ErrNotFound) {
r.logger.V(1).Info(fmt.Sprintf("policy %s not found, creating.", r.instance.Spec.PolicyID))
if r.ctx == nil {
r.logger.Error(errors.New("hejhejhej1"), "ctx is nil")
}
if r.osClient == nil {
r.logger.Error(errors.New("hejhejhej2"), "osclient is nil")
}
if ismpolicy == nil {
r.logger.Error(errors.New("hejhejhej3"), "ismpolicy is nil")
}
retErr = services.CreateISMPolicy(r.ctx, r.osClient, *ismpolicy, policyId, &r.logger)
retErr = services.CreateISMPolicy(r.ctx, r.osClient, *ismpolicy, policyId)
if retErr != nil {
reason = "failed to create ism policy"
r.logger.Error(retErr, spew.Sdump(*ismpolicy))
r.logger.Error(retErr, reason)
r.recorder.Event(r.instance, "Warning", opensearchAPIError, reason)
return
Expand Down

0 comments on commit 45f36ed

Please sign in to comment.