Skip to content

Commit 47d2ac0

Browse files
committed
Improve default label handling
Signed-off-by: Maksym Pavlenko <[email protected]>
1 parent 4e2fc81 commit 47d2ac0

1 file changed

Lines changed: 35 additions & 19 deletions

File tree

client.go

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,15 @@ func New(address string, opts ...ClientOpt) (*Client, error) {
8787
if copts.timeout == 0 {
8888
copts.timeout = 10 * time.Second
8989
}
90-
rt := fmt.Sprintf("%s.%s", plugin.RuntimePlugin, runtime.GOOS)
90+
91+
c := &Client{}
92+
9193
if copts.defaultRuntime != "" {
92-
rt = copts.defaultRuntime
93-
}
94-
c := &Client{
95-
runtime: rt,
94+
c.runtime = copts.defaultRuntime
95+
} else {
96+
c.runtime = fmt.Sprintf("%s.%s", plugin.RuntimePlugin, runtime.GOOS)
9697
}
98+
9799
if copts.services != nil {
98100
c.services = *copts.services
99101
}
@@ -140,13 +142,8 @@ func New(address string, opts ...ClientOpt) (*Client, error) {
140142

141143
// check namespace labels for default runtime
142144
if copts.defaultRuntime == "" && copts.defaultns != "" {
143-
namespaces := c.NamespaceService()
144-
ctx := context.Background()
145-
if labels, err := namespaces.Labels(ctx, copts.defaultns); err == nil {
146-
if defaultRuntime, ok := labels[defaults.DefaultRuntimeNSLabel]; ok {
147-
c.runtime = defaultRuntime
148-
}
149-
} else {
145+
ctx := namespaces.WithNamespace(context.Background(), copts.defaultns)
146+
if err := c.GetLabel(ctx, defaults.DefaultRuntimeNSLabel, &c.runtime, ""); err != nil {
150147
return nil, err
151148
}
152149
}
@@ -170,13 +167,8 @@ func NewWithConn(conn *grpc.ClientConn, opts ...ClientOpt) (*Client, error) {
170167

171168
// check namespace labels for default runtime
172169
if copts.defaultRuntime == "" && copts.defaultns != "" {
173-
namespaces := c.NamespaceService()
174-
ctx := context.Background()
175-
if labels, err := namespaces.Labels(ctx, copts.defaultns); err == nil {
176-
if defaultRuntime, ok := labels[defaults.DefaultRuntimeNSLabel]; ok {
177-
c.runtime = defaultRuntime
178-
}
179-
} else {
170+
ctx := namespaces.WithNamespace(context.Background(), copts.defaultns)
171+
if err := c.GetLabel(ctx, defaults.DefaultRuntimeNSLabel, &c.runtime, ""); err != nil {
180172
return nil, err
181173
}
182174
}
@@ -491,6 +483,30 @@ func writeIndex(ctx context.Context, index *ocispec.Index, client *Client, ref s
491483
return writeContent(ctx, client.ContentStore(), ocispec.MediaTypeImageIndex, ref, bytes.NewReader(data), content.WithLabels(labels))
492484
}
493485

486+
// GetLabel gets a label value from namespace store and saves it in 'out' variable.
487+
// If there is no value, a fallback value will be used instead.
488+
func (c *Client) GetLabel(ctx context.Context, label string, out *string, fallback string) error {
489+
ns, err := namespaces.NamespaceRequired(ctx)
490+
if err != nil {
491+
return err
492+
}
493+
494+
srv := c.NamespaceService()
495+
labels, err := srv.Labels(ctx, ns)
496+
if err != nil {
497+
return err
498+
}
499+
500+
value, ok := labels[label]
501+
if ok {
502+
*out = value
503+
} else {
504+
*out = fallback
505+
}
506+
507+
return nil
508+
}
509+
494510
// Subscribe to events that match one or more of the provided filters.
495511
//
496512
// Callers should listen on both the envelope and errs channels. If the errs

0 commit comments

Comments
 (0)