github.com/lalkh/containerd@v1.4.3/namespaces/context.go (about)

     1  /*
     2     Copyright The containerd Authors.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package namespaces
    18  
    19  import (
    20  	"context"
    21  	"os"
    22  
    23  	"github.com/containerd/containerd/errdefs"
    24  	"github.com/containerd/containerd/identifiers"
    25  	"github.com/pkg/errors"
    26  )
    27  
    28  const (
    29  	// NamespaceEnvVar is the environment variable key name
    30  	NamespaceEnvVar = "CONTAINERD_NAMESPACE"
    31  	// Default is the name of the default namespace
    32  	Default = "default"
    33  )
    34  
    35  type namespaceKey struct{}
    36  
    37  // WithNamespace sets a given namespace on the context
    38  func WithNamespace(ctx context.Context, namespace string) context.Context {
    39  	ctx = context.WithValue(ctx, namespaceKey{}, namespace) // set our key for namespace
    40  	// also store on the grpc and ttrpc headers so it gets picked up by any clients that
    41  	// are using this.
    42  	return withTTRPCNamespaceHeader(withGRPCNamespaceHeader(ctx, namespace), namespace)
    43  }
    44  
    45  // NamespaceFromEnv uses the namespace defined in CONTAINERD_NAMESPACE or
    46  // default
    47  func NamespaceFromEnv(ctx context.Context) context.Context {
    48  	namespace := os.Getenv(NamespaceEnvVar)
    49  	if namespace == "" {
    50  		namespace = Default
    51  	}
    52  	return WithNamespace(ctx, namespace)
    53  }
    54  
    55  // Namespace returns the namespace from the context.
    56  //
    57  // The namespace is not guaranteed to be valid.
    58  func Namespace(ctx context.Context) (string, bool) {
    59  	namespace, ok := ctx.Value(namespaceKey{}).(string)
    60  	if !ok {
    61  		if namespace, ok = fromGRPCHeader(ctx); !ok {
    62  			return fromTTRPCHeader(ctx)
    63  		}
    64  	}
    65  	return namespace, ok
    66  }
    67  
    68  // NamespaceRequired returns the valid namespace from the context or an error.
    69  func NamespaceRequired(ctx context.Context) (string, error) {
    70  	namespace, ok := Namespace(ctx)
    71  	if !ok || namespace == "" {
    72  		return "", errors.Wrapf(errdefs.ErrFailedPrecondition, "namespace is required")
    73  	}
    74  	if err := identifiers.Validate(namespace); err != nil {
    75  		return "", errors.Wrap(err, "namespace validation")
    76  	}
    77  	return namespace, nil
    78  }