github.com/jenkins-x/jx/v2@v2.1.155/pkg/kube/version.go (about)

     1  package kube
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/jenkins-x/jx/v2/pkg/kserving"
     7  	corev1 "k8s.io/api/core/v1"
     8  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
     9  )
    10  
    11  // GetVersion returns the version from the labels on the deployment if it can be deduced
    12  func GetVersion(r *metav1.ObjectMeta) string {
    13  	if r != nil {
    14  		labels := r.Labels
    15  		if labels != nil {
    16  			v := labels["version"]
    17  			if v != "" {
    18  				return v
    19  			}
    20  			v = labels["chart"]
    21  			if v != "" {
    22  				arr := strings.Split(v, "-")
    23  				last := arr[len(arr)-1]
    24  				if last != "" {
    25  					return last
    26  				}
    27  				return v
    28  			}
    29  
    30  			// find the kserve revision
    31  			kversion := labels[kserving.RevisionLabel]
    32  			if kversion != "" {
    33  				idx := strings.LastIndex(kversion, "-")
    34  				if idx > 0 {
    35  					kversion = kversion[idx+1:]
    36  				}
    37  				return kversion
    38  			}
    39  		}
    40  	}
    41  	return ""
    42  }
    43  
    44  // GetPodVersion returns the version for the given app name
    45  func GetPodVersion(pod *corev1.Pod, appName string) string {
    46  	v := GetVersion(&pod.ObjectMeta)
    47  	if v != "" {
    48  		return v
    49  	}
    50  	if appName == "" {
    51  		appName = GetName(&pod.ObjectMeta)
    52  	}
    53  	if appName != "" {
    54  		for _, c := range pod.Spec.Containers {
    55  			image := c.Image
    56  			idx := strings.LastIndex(image, ":")
    57  			if idx > 0 {
    58  				version := image[idx+1:]
    59  				prefix := image[0:idx]
    60  				if prefix == appName || strings.HasSuffix(prefix, "/"+appName) {
    61  					return version
    62  				}
    63  			}
    64  		}
    65  	}
    66  	return ""
    67  }
    68  
    69  // GetName returns the app name
    70  func GetName(r *metav1.ObjectMeta) string {
    71  	if r != nil {
    72  		ns := r.Namespace
    73  		labels := r.Labels
    74  		if labels != nil {
    75  			name := labels["app"]
    76  			if name != "" {
    77  				prefix := ns + "-"
    78  				if !strings.HasPrefix(name, prefix) {
    79  					prefix = "jx-"
    80  				}
    81  
    82  				// for helm deployments which prefix the namespace in the name lets strip it
    83  				if strings.HasPrefix(name, prefix) {
    84  					name = strings.TrimPrefix(name, prefix)
    85  
    86  					// we often have the app name repeated twice!
    87  					l := len(name) / 2
    88  					if name[l] == '-' {
    89  						first := name[0:l]
    90  						if name[l+1:] == first {
    91  							return first
    92  						}
    93  					}
    94  				}
    95  				return name
    96  			}
    97  		}
    98  		name := r.Name
    99  
   100  		if ns != "" {
   101  			prefix := ns + "-"
   102  			if !strings.HasPrefix(name, prefix) {
   103  				prefix = "jx-"
   104  			}
   105  
   106  			// for helm deployments which prefix the namespace in the name lets strip it
   107  			if strings.HasPrefix(name, prefix) {
   108  				name = strings.TrimPrefix(name, prefix)
   109  				return name
   110  			}
   111  		}
   112  		return name
   113  	}
   114  	return ""
   115  }
   116  
   117  // GetAppName returns the app name
   118  func GetAppName(name string, namespaces ...string) string {
   119  	if name != "" {
   120  		for _, ns := range namespaces {
   121  			// for helm deployments which prefix the namespace in the name lets strip it
   122  			prefix := ns + "-"
   123  			if strings.HasPrefix(name, prefix) {
   124  				name = strings.TrimPrefix(name, prefix)
   125  			}
   126  		}
   127  
   128  		// we often have the app name repeated twice - particularly when using helm 3
   129  		l := len(name) / 2
   130  		if name[l] == '-' {
   131  			first := name[0:l]
   132  			if name[l+1:] == first {
   133  				return first
   134  			}
   135  		}
   136  
   137  		// The applications seems to be prefixed with jx regardless of the namespace
   138  		// where they are deployed. Let's remove this prefix.
   139  		prefix := "jx-"
   140  		if strings.HasPrefix(name, prefix) {
   141  			name = strings.TrimPrefix(name, prefix)
   142  		}
   143  	}
   144  	return name
   145  }
   146  
   147  // GetCommitSha returns the git commit sha
   148  func GetCommitSha(r *metav1.ObjectMeta) string {
   149  	if r != nil {
   150  		annotations := r.Annotations
   151  		if annotations != nil {
   152  			return annotations["jenkins.io/git-sha"]
   153  		}
   154  	}
   155  	return ""
   156  }
   157  
   158  // GetCommitURL returns the git commit URL
   159  func GetCommitURL(r *metav1.ObjectMeta) string {
   160  	if r != nil {
   161  		annotations := r.Annotations
   162  		if annotations != nil {
   163  			return annotations["jenkins.io/git-url"]
   164  		}
   165  	}
   166  	return ""
   167  }
   168  
   169  func GetEditAppName(name string) string {
   170  	// we often have the app name repeated twice!
   171  	l := len(name) / 2
   172  	if name[l] == '-' {
   173  		first := name[0:l]
   174  		if name[l+1:] == first {
   175  			return first
   176  		}
   177  	}
   178  	return name
   179  }