Improve default label handling
Signed-off-by: Maksym Pavlenko <makpav@amazon.com>
This commit is contained in:
parent
4e2fc81edd
commit
47d2ac0902
54
client.go
54
client.go
@ -87,13 +87,15 @@ func New(address string, opts ...ClientOpt) (*Client, error) {
|
|||||||
if copts.timeout == 0 {
|
if copts.timeout == 0 {
|
||||||
copts.timeout = 10 * time.Second
|
copts.timeout = 10 * time.Second
|
||||||
}
|
}
|
||||||
rt := fmt.Sprintf("%s.%s", plugin.RuntimePlugin, runtime.GOOS)
|
|
||||||
|
c := &Client{}
|
||||||
|
|
||||||
if copts.defaultRuntime != "" {
|
if copts.defaultRuntime != "" {
|
||||||
rt = copts.defaultRuntime
|
c.runtime = copts.defaultRuntime
|
||||||
}
|
} else {
|
||||||
c := &Client{
|
c.runtime = fmt.Sprintf("%s.%s", plugin.RuntimePlugin, runtime.GOOS)
|
||||||
runtime: rt,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if copts.services != nil {
|
if copts.services != nil {
|
||||||
c.services = *copts.services
|
c.services = *copts.services
|
||||||
}
|
}
|
||||||
@ -140,13 +142,8 @@ func New(address string, opts ...ClientOpt) (*Client, error) {
|
|||||||
|
|
||||||
// check namespace labels for default runtime
|
// check namespace labels for default runtime
|
||||||
if copts.defaultRuntime == "" && copts.defaultns != "" {
|
if copts.defaultRuntime == "" && copts.defaultns != "" {
|
||||||
namespaces := c.NamespaceService()
|
ctx := namespaces.WithNamespace(context.Background(), copts.defaultns)
|
||||||
ctx := context.Background()
|
if err := c.GetLabel(ctx, defaults.DefaultRuntimeNSLabel, &c.runtime, ""); err != nil {
|
||||||
if labels, err := namespaces.Labels(ctx, copts.defaultns); err == nil {
|
|
||||||
if defaultRuntime, ok := labels[defaults.DefaultRuntimeNSLabel]; ok {
|
|
||||||
c.runtime = defaultRuntime
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -170,13 +167,8 @@ func NewWithConn(conn *grpc.ClientConn, opts ...ClientOpt) (*Client, error) {
|
|||||||
|
|
||||||
// check namespace labels for default runtime
|
// check namespace labels for default runtime
|
||||||
if copts.defaultRuntime == "" && copts.defaultns != "" {
|
if copts.defaultRuntime == "" && copts.defaultns != "" {
|
||||||
namespaces := c.NamespaceService()
|
ctx := namespaces.WithNamespace(context.Background(), copts.defaultns)
|
||||||
ctx := context.Background()
|
if err := c.GetLabel(ctx, defaults.DefaultRuntimeNSLabel, &c.runtime, ""); err != nil {
|
||||||
if labels, err := namespaces.Labels(ctx, copts.defaultns); err == nil {
|
|
||||||
if defaultRuntime, ok := labels[defaults.DefaultRuntimeNSLabel]; ok {
|
|
||||||
c.runtime = defaultRuntime
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -491,6 +483,30 @@ func writeIndex(ctx context.Context, index *ocispec.Index, client *Client, ref s
|
|||||||
return writeContent(ctx, client.ContentStore(), ocispec.MediaTypeImageIndex, ref, bytes.NewReader(data), content.WithLabels(labels))
|
return writeContent(ctx, client.ContentStore(), ocispec.MediaTypeImageIndex, ref, bytes.NewReader(data), content.WithLabels(labels))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetLabel gets a label value from namespace store and saves it in 'out' variable.
|
||||||
|
// If there is no value, a fallback value will be used instead.
|
||||||
|
func (c *Client) GetLabel(ctx context.Context, label string, out *string, fallback string) error {
|
||||||
|
ns, err := namespaces.NamespaceRequired(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
srv := c.NamespaceService()
|
||||||
|
labels, err := srv.Labels(ctx, ns)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
value, ok := labels[label]
|
||||||
|
if ok {
|
||||||
|
*out = value
|
||||||
|
} else {
|
||||||
|
*out = fallback
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Subscribe to events that match one or more of the provided filters.
|
// Subscribe to events that match one or more of the provided filters.
|
||||||
//
|
//
|
||||||
// Callers should listen on both the envelope and errs channels. If the errs
|
// Callers should listen on both the envelope and errs channels. If the errs
|
||||||
|
Loading…
Reference in New Issue
Block a user