-
Notifications
You must be signed in to change notification settings - Fork 772
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
list custom resource managed resources #634
Open
petar-cvit
wants to merge
8
commits into
main
Choose a base branch
from
crd-managed-resources
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9af4a27
list custom resource managed resources
petar-cvit 3a4312c
identify child label by crd gvk
petar-cvit 6d392f5
add use state deps
petar-cvit 3033505
add use callback deps
petar-cvit 32205d5
child labels configmap
petar-cvit 85e8c24
remove print
petar-cvit 9b9480d
fetch resource relations from configmap
petar-cvit f220b9e
remove volume mount
petar-cvit 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ DISABLE_TELEMETRY=true | |
PORT=8888 | ||
WATCH_NAMESPACE=cyclops | ||
CYCLOPS_VERSION=v0.0.0 | ||
CONFIG_PATH=config.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,67 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/cyclops-ui/cyclops/cyclops-ctrl/pkg/cluster/k8sclient" | ||
"gopkg.in/yaml.v3" | ||
"io" | ||
"os" | ||
) | ||
|
||
type ControllerConfig struct { | ||
ChildLabels k8sclient.ChildLabels `json:"childLabels"` | ||
} | ||
|
||
func getConfig() ControllerConfig { | ||
configPath := os.Getenv("CONFIG_PATH") | ||
if configPath == "" { | ||
configPath = "/etc/config/config.yaml" | ||
} | ||
|
||
configFile, err := os.Open(configPath) | ||
if err != nil { | ||
setupLog.Error(err, "Failed to open controller config", "configPath", configPath) | ||
return ControllerConfig{} | ||
} | ||
defer configFile.Close() | ||
|
||
b, err := io.ReadAll(configFile) | ||
if err != nil { | ||
setupLog.Error(err, "Failed to read controller config", "configPath", configPath) | ||
return ControllerConfig{} | ||
} | ||
|
||
type resourceChildLabels struct { | ||
Group string `yaml:"group"` | ||
Version string `yaml:"version"` | ||
Kind string `yaml:"kind"` | ||
MatchLabels map[string]string `yaml:"matchLabels"` | ||
} | ||
|
||
type yamlConfig struct { | ||
ChildLabels []resourceChildLabels `yaml:"childLabels"` | ||
} | ||
|
||
var c *yamlConfig | ||
err = yaml.Unmarshal(b, &c) | ||
if err != nil { | ||
setupLog.Error(err, "Failed to YAML unmarshal controller config", "configPath", configPath) | ||
return ControllerConfig{} | ||
} | ||
|
||
if c == nil { | ||
return ControllerConfig{} | ||
} | ||
|
||
childLabels := make(map[k8sclient.GroupVersionKind]map[string]string) | ||
for _, label := range c.ChildLabels { | ||
childLabels[k8sclient.GroupVersionKind{ | ||
Group: label.Group, | ||
Version: label.Version, | ||
Kind: label.Kind, | ||
}] = label.MatchLabels | ||
} | ||
|
||
return ControllerConfig{ | ||
ChildLabels: childLabels, | ||
} | ||
} |
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,6 @@ | ||
childLabels: | ||
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. Add comment so that people know it's an example for testing |
||
- group: postgresql.cnpg.io | ||
version: v1 | ||
kind: Cluster | ||
matchLabels: | ||
cnpg.io/cluster: "{{ .metadata.name }}" |
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,53 @@ | ||
package k8sclient | ||
|
||
import ( | ||
"bytes" | ||
"text/template" | ||
|
||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
) | ||
|
||
const ( | ||
cyclopsNamespace = "cyclops" | ||
) | ||
|
||
type GroupVersionKind struct { | ||
Group string `json:"group"` | ||
Version string `json:"version"` | ||
Kind string `json:"kind"` | ||
} | ||
|
||
type ChildLabels map[GroupVersionKind]map[string]string | ||
|
||
func (k *KubernetesClient) getChildLabel( | ||
group, version, kind string, | ||
obj *unstructured.Unstructured, | ||
) (map[string]string, bool, error) { | ||
labels, exists := k.childLabels[GroupVersionKind{ | ||
Group: group, | ||
Version: version, | ||
Kind: kind, | ||
}] | ||
|
||
if !exists { | ||
return nil, false, nil | ||
} | ||
|
||
matchLabels := make(map[string]string) | ||
for k, v := range labels { | ||
t, err := template.New("matchLabel").Parse(v) | ||
if err != nil { | ||
return nil, false, err | ||
} | ||
|
||
var o bytes.Buffer | ||
err = t.Execute(&o, obj.Object) | ||
if err != nil { | ||
return nil, false, err | ||
} | ||
|
||
matchLabels[k] = o.String() | ||
} | ||
|
||
return matchLabels, exists, 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
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
Oops, something went wrong.
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.
This function is not in use