generated from layer5io/layer5-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 47
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
Meshync Dynamic Configuration from Meshsync #258
Merged
MUzairS15
merged 23 commits into
meshery:master
from
KiptoonKipkurui:feat/kiptoonkipkurui/meshsync_crd
Nov 8, 2023
Merged
Changes from 20 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
63ab2a3
add MeshsyncConfig used to receive config from meshync
KiptoonKipkurui 8fecc42
add functionality to process config from
KiptoonKipkurui c2f557e
refactor to obtain incoporate configs from meshsync
KiptoonKipkurui 80d8263
Merge branch 'master' into feat/kiptoonkipkurui/meshsync_crd
KiptoonKipkurui 439496a
ensure the cr is present before using it
KiptoonKipkurui 2ee712f
check conversion of spec for success
KiptoonKipkurui 543b383
Merge branch 'meshery:master' into feat/kiptoonkipkurui/meshsync_crd
KiptoonKipkurui 55d3cc3
refactor to add checks before using spec map
KiptoonKipkurui 3dcd825
update variable namings for better understandability
KiptoonKipkurui 042eb80
add slice package
KiptoonKipkurui 46256a9
add watched resource model
KiptoonKipkurui c82b97c
refactor to allow configuration of resources
KiptoonKipkurui 4837992
check whether an event type is supported before
KiptoonKipkurui 95c9d15
check for presence of configs from piplines and
KiptoonKipkurui 0e99363
safe initialize pipeline
KiptoonKipkurui d1b0c76
go mod tidy package management
KiptoonKipkurui a01a14a
refactor to supply dynamic kubernetes client
KiptoonKipkurui 6d68702
add unit tests for crd configuration
KiptoonKipkurui be975d8
add default events to be tracked
KiptoonKipkurui 077384f
refactor for easier testing
KiptoonKipkurui 99d89b2
ensure either whitelist or blacklisted resources allowed
KiptoonKipkurui 89d41ad
Merge branch 'master' into feat/kiptoonkipkurui/meshsync_crd
KiptoonKipkurui 988de5b
update packages to get dynamic kubernetes go client
KiptoonKipkurui 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
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
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,186 @@ | ||||||
package config | ||||||
|
||||||
import ( | ||||||
"context" | ||||||
"errors" | ||||||
|
||||||
"github.com/layer5io/meshkit/utils" | ||||||
"golang.org/x/exp/slices" | ||||||
corev1 "k8s.io/api/core/v1" | ||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||||||
"k8s.io/apimachinery/pkg/runtime/schema" | ||||||
"k8s.io/client-go/dynamic" | ||||||
) | ||||||
|
||||||
var ( | ||||||
namespace = "meshery" // Namespace for the Custom Resource | ||||||
crName = "meshery-meshsync" // Name of the custom resource | ||||||
version = "v1alpha1" // Version of the Custom Resource | ||||||
group = "meshery.layer5.io" //Group for the Custom Resource | ||||||
resource = "meshsyncs" //Name of the Resource | ||||||
) | ||||||
|
||||||
func GetMeshsyncCRDConfigs(dyClient dynamic.Interface) (*MeshsyncConfig, error) { | ||||||
// initialize the group version resource to access the custom resource | ||||||
gvr := schema.GroupVersionResource{Version: version, Group: group, Resource: resource} | ||||||
|
||||||
// make a call to get the custom resource | ||||||
crd, err := dyClient.Resource(gvr).Namespace(namespace).Get(context.TODO(), crName, metav1.GetOptions{}) | ||||||
|
||||||
if err != nil { | ||||||
return nil, ErrInitConfig(err) | ||||||
} | ||||||
|
||||||
KiptoonKipkurui marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
if crd == nil { | ||||||
return nil, ErrInitConfig(errors.New("Custom Resource is nil")) | ||||||
} | ||||||
|
||||||
spec := crd.Object["spec"] | ||||||
specMap, ok := spec.(map[string]interface{}) | ||||||
if !ok { | ||||||
return nil, ErrInitConfig(errors.New("Unable to convert spec to map")) | ||||||
} | ||||||
configObj := specMap["watch-list"] | ||||||
if configObj == nil { | ||||||
return nil, ErrInitConfig(errors.New("Custom Resource does not have Meshsync Configs")) | ||||||
} | ||||||
configStr, err := utils.Marshal(configObj) | ||||||
if err != nil { | ||||||
return nil, ErrInitConfig(err) | ||||||
} | ||||||
|
||||||
configMap := corev1.ConfigMap{} | ||||||
err = utils.Unmarshal(string(configStr), &configMap) | ||||||
|
||||||
if err != nil { | ||||||
return nil, ErrInitConfig(err) | ||||||
} | ||||||
|
||||||
// populate the required configs | ||||||
meshsyncConfig, err := PopulateConfigs(configMap) | ||||||
|
||||||
if err != nil { | ||||||
return nil, ErrInitConfig(err) | ||||||
} | ||||||
return meshsyncConfig, nil | ||||||
} | ||||||
|
||||||
// PopulateConfigs compares the default configs and the whitelist and blacklist | ||||||
func PopulateConfigs(configMap corev1.ConfigMap) (*MeshsyncConfig, error) { | ||||||
meshsyncConfig := &MeshsyncConfig{} | ||||||
|
||||||
if _, ok := configMap.Data["blacklist"]; ok { | ||||||
if len(configMap.Data["blacklist"]) > 0 { | ||||||
err := utils.Unmarshal(configMap.Data["blacklist"], &meshsyncConfig.BlackList) | ||||||
if err != nil { | ||||||
return nil, ErrInitConfig(err) | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
if _, ok := configMap.Data["whitelist"]; ok { | ||||||
if len(configMap.Data["whitelist"]) > 0 { | ||||||
err := utils.Unmarshal(configMap.Data["whitelist"], &meshsyncConfig.WhiteList) | ||||||
if err != nil { | ||||||
return nil, ErrInitConfig(err) | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
// ensure that atleast one of whitelist or blacklist has been supplied | ||||||
if len(meshsyncConfig.BlackList) == 0 && len(meshsyncConfig.WhiteList) == 0 { | ||||||
return nil, ErrInitConfig(errors.New("Both whitelisted and blacklisted resources missing")) | ||||||
} | ||||||
|
||||||
// Handle global resources | ||||||
globalPipelines := make(PipelineConfigs, 0) | ||||||
localPipelines := make(PipelineConfigs, 0) | ||||||
|
||||||
if len(meshsyncConfig.WhiteList) != 0 { | ||||||
for _, v := range Pipelines[GlobalResourceKey] { | ||||||
if idx := slices.IndexFunc(meshsyncConfig.WhiteList, func(c ResourceConfig) bool { return c.Resource == v.Name }); idx != -1 { | ||||||
config := meshsyncConfig.WhiteList[idx] | ||||||
v.Events = config.Events | ||||||
globalPipelines = append(globalPipelines, v) | ||||||
} | ||||||
} | ||||||
if len(globalPipelines) > 0 { | ||||||
meshsyncConfig.Pipelines = map[string]PipelineConfigs{} | ||||||
meshsyncConfig.Pipelines[GlobalResourceKey] = globalPipelines | ||||||
} | ||||||
|
||||||
// Handle local resources | ||||||
for _, v := range Pipelines[LocalResourceKey] { | ||||||
if idx := slices.IndexFunc(meshsyncConfig.WhiteList, func(c ResourceConfig) bool { return c.Resource == v.Name }); idx != -1 { | ||||||
config := meshsyncConfig.WhiteList[idx] | ||||||
v.Events = config.Events | ||||||
localPipelines = append(localPipelines, v) | ||||||
} | ||||||
} | ||||||
|
||||||
if len(localPipelines) > 0 { | ||||||
if meshsyncConfig.Pipelines == nil { | ||||||
meshsyncConfig.Pipelines = make(map[string]PipelineConfigs) | ||||||
} | ||||||
meshsyncConfig.Pipelines[LocalResourceKey] = localPipelines | ||||||
} | ||||||
|
||||||
// Handle listeners | ||||||
listerners := make(ListenerConfigs, 0) | ||||||
for _, v := range Listeners { | ||||||
if idx := slices.IndexFunc(meshsyncConfig.WhiteList, func(c ResourceConfig) bool { return c.Resource == v.Name }); idx != -1 { | ||||||
config := meshsyncConfig.WhiteList[idx] | ||||||
v.Events = config.Events | ||||||
listerners = append(listerners, v) | ||||||
} | ||||||
} | ||||||
|
||||||
if len(listerners) > 0 { | ||||||
meshsyncConfig.Listeners = make(map[string]ListenerConfig) | ||||||
meshsyncConfig.Listeners = Listeners | ||||||
} | ||||||
} else if len(meshsyncConfig.BlackList) != 0 { | ||||||
|
||||||
for _, v := range Pipelines[GlobalResourceKey] { | ||||||
if idx := slices.IndexFunc(meshsyncConfig.BlackList, func(c string) bool { return c == v.Name }); idx == -1 { | ||||||
v.Events = DefaultEvents | ||||||
globalPipelines = append(globalPipelines, v) | ||||||
} | ||||||
} | ||||||
if len(globalPipelines) > 0 { | ||||||
meshsyncConfig.Pipelines = map[string]PipelineConfigs{} | ||||||
meshsyncConfig.Pipelines[GlobalResourceKey] = globalPipelines | ||||||
} | ||||||
|
||||||
// Handle local resources | ||||||
for _, v := range Pipelines[LocalResourceKey] { | ||||||
if idx := slices.IndexFunc(meshsyncConfig.BlackList, func(c string) bool { return c == v.Name }); idx == -1 { | ||||||
v.Events = DefaultEvents | ||||||
localPipelines = append(localPipelines, v) | ||||||
} | ||||||
} | ||||||
|
||||||
if len(localPipelines) > 0 { | ||||||
if meshsyncConfig.Pipelines == nil { | ||||||
meshsyncConfig.Pipelines = make(map[string]PipelineConfigs) | ||||||
} | ||||||
meshsyncConfig.Pipelines[LocalResourceKey] = localPipelines | ||||||
} | ||||||
|
||||||
// Handle listeners | ||||||
listerners := make(ListenerConfigs, 0) | ||||||
for _, v := range Listeners { | ||||||
if idx := slices.IndexFunc(meshsyncConfig.BlackList, func(c string) bool { return c == v.Name }); idx != -1 { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
== I believe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Resolved by removing listeners till further conversations |
||||||
v.Events = DefaultEvents | ||||||
listerners = append(listerners, v) | ||||||
} | ||||||
} | ||||||
|
||||||
if len(listerners) > 0 { | ||||||
meshsyncConfig.Listeners = make(map[string]ListenerConfig) | ||||||
meshsyncConfig.Listeners = Listeners | ||||||
} | ||||||
} | ||||||
|
||||||
return meshsyncConfig, nil | ||||||
} |
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.
Maybe you can import it rather than hard coded it https://github.com/meshery/meshery-operator/blob/master/api/v1alpha1/meshsync_types.go. Same like
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
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.
I will update meshsync with this change immediately the meshery operator is approved with the CRD changes