github.com/GoogleContainerTools/skaffold/v2@v2.13.2/pkg/diag/validator/resource.go (about)

     1  /*
     2  Copyright 2020 The Skaffold 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 validator
    18  
    19  import (
    20  	"fmt"
    21  
    22  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    23  	"k8s.io/apimachinery/pkg/runtime"
    24  
    25  	"github.com/GoogleContainerTools/skaffold/v2/proto/v1"
    26  )
    27  
    28  type Resource struct {
    29  	namespace string
    30  	kind      string
    31  	name      string
    32  	logs      []string
    33  	status    Status
    34  	ae        *proto.ActionableErr
    35  }
    36  
    37  func (r Resource) Kind() string      { return r.kind }
    38  func (r Resource) Name() string      { return r.name }
    39  func (r Resource) Namespace() string { return r.namespace }
    40  func (r Resource) Status() Status    { return r.status }
    41  func (r Resource) Logs() []string    { return r.logs }
    42  func (r Resource) String() string {
    43  	if r.namespace == "default" {
    44  		return fmt.Sprintf("%s/%s", r.kind, r.name)
    45  	}
    46  	return fmt.Sprintf("%s:%s/%s", r.namespace, r.kind, r.name)
    47  }
    48  func (r Resource) ActionableError() *proto.ActionableErr {
    49  	return r.ae
    50  }
    51  func (r Resource) StatusUpdated(another Resource) bool {
    52  	return r.ae.ErrCode != another.ae.ErrCode
    53  }
    54  
    55  // NewResource creates new Resource of kind
    56  func NewResource(namespace, kind, name string, status Status, ae *proto.ActionableErr, logs []string) Resource {
    57  	return Resource{namespace: namespace, kind: kind, name: name, status: status, ae: ae, logs: logs}
    58  }
    59  
    60  // objectWithMetadata is any k8s object that has kind and object metadata.
    61  type objectWithMetadata interface {
    62  	runtime.Object
    63  	metav1.Object
    64  }
    65  
    66  // NewResourceFromObject creates new Resource with fields populated from object metadata.
    67  func NewResourceFromObject(object objectWithMetadata, status Status, ae *proto.ActionableErr, logs []string) Resource {
    68  	return NewResource(object.GetNamespace(), object.GetObjectKind().GroupVersionKind().Kind, object.GetName(), status, ae, logs)
    69  }