-
Notifications
You must be signed in to change notification settings - Fork 9
add service linking for deploio apps #388
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
Merged
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,56 @@ | ||
| package application | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| meta "github.com/ninech/apis/meta/v1alpha1" | ||
| "github.com/ninech/nctl/api" | ||
| "github.com/ninech/nctl/internal/cli" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/runtime/schema" | ||
| ) | ||
|
|
||
| // TypedReference is a reference to a resource with a specific type. | ||
| // It implements encoding.TextUnmarshaler to parse "kind/name" strings, | ||
| // which allows it to be used directly as a Kong flag type. | ||
| type TypedReference struct { | ||
| meta.TypedReference | ||
| } | ||
|
|
||
| // UnmarshalText parses a typed reference from a string in "kind/name" format. | ||
| func (r *TypedReference) UnmarshalText(text []byte) error { | ||
| s := strings.TrimSpace(string(text)) | ||
| kind, name, found := strings.Cut(s, "/") | ||
| if !found || kind == "" || name == "" { | ||
| return fmt.Errorf("unmarshal error: expected kind/name, got %q", text) | ||
| } | ||
|
|
||
| gvk, err := GroupVersionKindFromKind(kind) | ||
| if err != nil { | ||
| return fmt.Errorf("unmarshal error: %w", err) | ||
| } | ||
|
|
||
| r.Name = name | ||
| r.GroupKind = metav1.GroupKind(gvk.GroupKind()) | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // GroupVersionKindFromKind resolves a case-insensitive kind string to a | ||
| // schema.GroupVersionKind using the registered scheme. | ||
| func GroupVersionKindFromKind(kind string) (schema.GroupVersionKind, error) { | ||
| scheme, err := api.NewScheme() | ||
| if err != nil { | ||
| return schema.GroupVersionKind{}, fmt.Errorf("error creating scheme: %w", err) | ||
| } | ||
|
|
||
| for gvk := range scheme.AllKnownTypes() { | ||
| if strings.EqualFold(kind, gvk.Kind) { | ||
| return gvk, nil | ||
| } | ||
| } | ||
|
|
||
| return schema.GroupVersionKind{}, cli.ErrorWithContext(fmt.Errorf("kind %q is invalid", kind)). | ||
| WithExitCode(cli.ExitUsageError) | ||
| } |
This file contains hidden or 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 application | ||
|
|
||
| import ( | ||
| "cmp" | ||
| "slices" | ||
|
|
||
| apps "github.com/ninech/apis/apps/v1alpha1" | ||
| "github.com/ninech/nctl/internal/format" | ||
| ) | ||
|
|
||
| // ServiceMap is a map of service name to typed reference, used as a CLI flag type. | ||
| type ServiceMap map[string]TypedReference | ||
|
|
||
| // ServicesFromMap converts a map of name -> TypedReference into a | ||
| // NamedServiceTargetList. The namespace is set on each target. | ||
| func ServicesFromMap(services ServiceMap, namespace string) apps.NamedServiceTargetList { | ||
| if len(services) == 0 { | ||
| return nil | ||
| } | ||
|
|
||
| result := make(apps.NamedServiceTargetList, 0, len(services)) | ||
| for name, ref := range services { | ||
| ref.Namespace = namespace | ||
| result = append(result, apps.NamedServiceTarget{ | ||
| Name: name, | ||
| Target: ref.TypedReference, | ||
| }) | ||
| } | ||
|
|
||
| slices.SortFunc(result, func(a, b apps.NamedServiceTarget) int { | ||
| return cmp.Compare(a.Name, b.Name) | ||
| }) | ||
|
|
||
| return result | ||
| } | ||
|
|
||
| // UpdateServices merges toAdd into existing services (upsert by name) and | ||
| // removes services listed in toDelete. Warnings are emitted for delete-not-found cases. | ||
| func UpdateServices(existing apps.NamedServiceTargetList, toAdd apps.NamedServiceTargetList, toDelete []string, w format.Writer) apps.NamedServiceTargetList { | ||
| // upsert: update existing or append new | ||
| for _, add := range toAdd { | ||
| found := false | ||
| for i := range existing { | ||
| if existing[i].Name == add.Name { | ||
| existing[i].Target = add.Target | ||
| found = true | ||
| break | ||
| } | ||
| } | ||
| if !found { | ||
| existing = append(existing, add) | ||
| } | ||
| } | ||
|
|
||
| // delete | ||
|
gajicdev marked this conversation as resolved.
|
||
| for _, name := range toDelete { | ||
| before := len(existing) | ||
| existing = slices.DeleteFunc(existing, func(s apps.NamedServiceTarget) bool { | ||
| return s.Name == name | ||
| }) | ||
| if len(existing) == before { | ||
| w.Warningf("did not find a service with the name %q", name) | ||
| } | ||
| } | ||
|
|
||
| return existing | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.