-
Notifications
You must be signed in to change notification settings - Fork 0
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-522,implementation for OpenSearch Egress Rules lifecycle on APIv2 #527
Merged
ribaraka
merged 1 commit into
main
from
issue-522-implementation-for-OpenSearch-Egress-Rules-lifecycle-workflow-on-APIv2
Oct 26, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
apis/clusterresources/v1beta1/opensearchegressrules_types.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
Copyright 2022. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package v1beta1 | ||
|
||
import ( | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
type OpenSearchEgressRulesSpec struct { | ||
ClusterID string `json:"clusterId"` | ||
OpenSearchBindingID string `json:"openSearchBindingId"` | ||
Source string `json:"source"` | ||
Type string `json:"type,omitempty"` | ||
} | ||
|
||
type OpenSearchEgressRulesStatus struct { | ||
ID string `json:"id,omitempty"` | ||
} | ||
|
||
//+kubebuilder:object:root=true | ||
//+kubebuilder:subresource:status | ||
|
||
// OpenSearchEgressRules is the Schema for the opensearchegressrules API | ||
type OpenSearchEgressRules struct { | ||
metav1.TypeMeta `json:",inline"` | ||
metav1.ObjectMeta `json:"metadata,omitempty"` | ||
|
||
Spec OpenSearchEgressRulesSpec `json:"spec,omitempty"` | ||
Status OpenSearchEgressRulesStatus `json:"status,omitempty"` | ||
} | ||
|
||
//+kubebuilder:object:root=true | ||
|
||
// OpenSearchEgressRulesList contains a list of OpenSearchEgressRules | ||
type OpenSearchEgressRulesList struct { | ||
metav1.TypeMeta `json:",inline"` | ||
metav1.ListMeta `json:"metadata,omitempty"` | ||
Items []OpenSearchEgressRules `json:"items"` | ||
} | ||
|
||
func (er *OpenSearchEgressRules) NewPatch() client.Patch { | ||
old := er.DeepCopy() | ||
return client.MergeFrom(old) | ||
} | ||
|
||
func init() { | ||
SchemeBuilder.Register(&OpenSearchEgressRules{}, &OpenSearchEgressRulesList{}) | ||
} |
93 changes: 93 additions & 0 deletions
93
apis/clusterresources/v1beta1/opensearchegressrules_webhook.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
Copyright 2022. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package v1beta1 | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
|
||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/utils/strings/slices" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
logf "sigs.k8s.io/controller-runtime/pkg/log" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook" | ||
|
||
"github.com/instaclustr/operator/pkg/models" | ||
) | ||
|
||
// log is for logging in this package. | ||
|
||
var opensearchegressruleslog = logf.Log.WithName("opensearchegressrules-resource") | ||
|
||
var destinationTypes = []string{"SLACK", "WEBHOOK", "CUSTOM_WEBHOOK", "CHIME"} | ||
var sourcePlugins = []string{"NOTIFICATIONS", "ALERTING"} | ||
|
||
func (r *OpenSearchEgressRules) SetupWebhookWithManager(mgr ctrl.Manager) error { | ||
return ctrl.NewWebhookManagedBy(mgr). | ||
For(r). | ||
Complete() | ||
} | ||
|
||
//+kubebuilder:webhook:path=/validate-clusterresources-instaclustr-com-v1beta1-opensearchegressrules,mutating=false,failurePolicy=fail,sideEffects=None,groups=clusterresources.instaclustr.com,resources=opensearchegressrules,verbs=create;update,versions=v1beta1,name=vopensearchegressrules.kb.io,admissionReviewVersions=v1 | ||
|
||
var _ webhook.Validator = &OpenSearchEgressRules{} | ||
|
||
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type | ||
func (r *OpenSearchEgressRules) ValidateCreate() error { | ||
opensearchegressruleslog.Info("validate create", "name", r.Name) | ||
matched, err := regexp.MatchString(models.OpenSearchBindingIDPattern, r.Spec.OpenSearchBindingID) | ||
if err != nil { | ||
return fmt.Errorf("can`t match openSearchBindingId to pattern: %s, error: %w", models.OpenSearchBindingIDPattern, err) | ||
} | ||
if !matched { | ||
return fmt.Errorf("mismatching openSearchBindingId to pattern: %s", models.OpenSearchBindingIDPattern) | ||
} | ||
|
||
if !slices.Contains(sourcePlugins, r.Spec.Source) { | ||
return fmt.Errorf("the source should be equal to one of the options: %q , got: %q", sourcePlugins, r.Spec.Source) | ||
} | ||
|
||
if !slices.Contains(destinationTypes, r.Spec.Type) { | ||
return fmt.Errorf("the type should be equal to one of the options: %q , got: %q", destinationTypes, r.Spec.Type) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type | ||
func (r *OpenSearchEgressRules) ValidateUpdate(old runtime.Object) error { | ||
opensearchegressruleslog.Info("validate update", "name", r.Name) | ||
|
||
oldRules := old.(*OpenSearchEgressRules) | ||
|
||
if r.Status.ID == "" { | ||
return r.ValidateCreate() | ||
} | ||
|
||
if r.Spec != oldRules.Spec { | ||
return models.ErrImmutableSpec | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type | ||
func (r *OpenSearchEgressRules) ValidateDelete() error { | ||
opensearchegressruleslog.Info("validate delete", "name", r.Name) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
config/crd/bases/clusterresources.instaclustr.com_opensearchegressrules.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
--- | ||
apiVersion: apiextensions.k8s.io/v1 | ||
kind: CustomResourceDefinition | ||
metadata: | ||
annotations: | ||
controller-gen.kubebuilder.io/version: v0.9.2 | ||
creationTimestamp: null | ||
name: opensearchegressrules.clusterresources.instaclustr.com | ||
spec: | ||
group: clusterresources.instaclustr.com | ||
names: | ||
kind: OpenSearchEgressRules | ||
listKind: OpenSearchEgressRulesList | ||
plural: opensearchegressrules | ||
singular: opensearchegressrules | ||
scope: Namespaced | ||
versions: | ||
- name: v1beta1 | ||
schema: | ||
openAPIV3Schema: | ||
description: OpenSearchEgressRules is the Schema for the opensearchegressrules | ||
API | ||
properties: | ||
apiVersion: | ||
description: 'APIVersion defines the versioned schema of this representation | ||
of an object. Servers should convert recognized schemas to the latest | ||
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources' | ||
type: string | ||
kind: | ||
description: 'Kind is a string value representing the REST resource this | ||
object represents. Servers may infer this from the endpoint the client | ||
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds' | ||
type: string | ||
metadata: | ||
type: object | ||
spec: | ||
properties: | ||
clusterId: | ||
type: string | ||
openSearchBindingId: | ||
type: string | ||
source: | ||
type: string | ||
type: | ||
type: string | ||
required: | ||
- clusterId | ||
- openSearchBindingId | ||
- source | ||
type: object | ||
status: | ||
properties: | ||
id: | ||
type: string | ||
type: object | ||
type: object | ||
served: true | ||
storage: true | ||
subresources: | ||
status: {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I am not wrong there are no API calls to update this entity.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But there are no API calls to update entity, only creation when resource has wrong configuration, to avoid additional operations, like deletion and creation new one, we just can edit resorce and apply it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How we will update resouce and apply it if there are no such API calls? How we will update it on instaclustr side? In such way you sad k8s will be updated but instaclustr part won't be updated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to decline the update of this resource. Without validation, if the customer tries to update this entity inside K8s and this action is passed successfully, it can be really confusing if the entity will not be updated on the Instaclustr API, but updated inside K8s