github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/kubernetes/portforward/port_forward_entry.go (about)

     1  /*
     2  Copyright 2019 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 portforward
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"strings"
    23  	"sync"
    24  
    25  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
    26  )
    27  
    28  type portForwardEntry struct {
    29  	resourceVersion        int
    30  	resource               latest.PortForwardResource
    31  	podName                string
    32  	containerName          string
    33  	portName               string
    34  	ownerReference         string
    35  	localPort              int
    36  	automaticPodForwarding bool
    37  	terminated             bool
    38  	terminationLock        sync.Mutex
    39  	cancel                 context.CancelFunc
    40  }
    41  
    42  // newPortForwardEntry returns a port forward entry.
    43  func newPortForwardEntry(resourceVersion int, resource latest.PortForwardResource, podName, containerName, portName, ownerReference string, localPort int, automaticPodForwarding bool) *portForwardEntry {
    44  	return &portForwardEntry{
    45  		resourceVersion:        resourceVersion,
    46  		resource:               resource,
    47  		podName:                podName,
    48  		containerName:          containerName,
    49  		portName:               portName,
    50  		ownerReference:         ownerReference,
    51  		localPort:              localPort,
    52  		automaticPodForwarding: automaticPodForwarding,
    53  	}
    54  }
    55  
    56  // key is an identifier for the lock on a port during the skaffold dev cycle.
    57  // if automaticPodForwarding is set, we return a key that doesn't include podName, since we want the key
    58  // to be the same whenever pods restart
    59  func (p *portForwardEntry) key() string {
    60  	if p.automaticPodForwarding {
    61  		return fmt.Sprintf("%s-%s-%s-%s-%s", p.ownerReference, p.containerName, p.resource.Namespace, p.portName, p.resource.Port.String())
    62  	}
    63  	return fmt.Sprintf("%s-%s-%s-%s", strings.ToLower(string(p.resource.Type)), p.resource.Name, p.resource.Namespace, p.resource.Port.String())
    64  }
    65  
    66  // String is a utility function that returns the port forward entry as a user-readable string
    67  func (p *portForwardEntry) String() string {
    68  	return fmt.Sprintf("%s-%s-%s-%s", strings.ToLower(string(p.resource.Type)), p.resource.Name, p.resource.Namespace, p.resource.Port.String())
    69  }