github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/docker/tracker/tracker.go (about)

     1  /*
     2  Copyright 2021 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 tracker
    18  
    19  import (
    20  	"sync"
    21  
    22  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/graph"
    23  )
    24  
    25  type Container struct {
    26  	Name string
    27  	ID   string
    28  }
    29  
    30  type ContainerTracker struct {
    31  	sync.RWMutex
    32  	deployedContainers  map[string]Container      // artifact image name -> container
    33  	containerToArtifact map[string]graph.Artifact // container id -> artifact (for colorpicker)
    34  	containers          map[string]bool           // set of tracked container ids
    35  	notifier            chan string
    36  }
    37  
    38  // NewContainerTracker creates a new ContainerTracker.
    39  func NewContainerTracker() *ContainerTracker {
    40  	return &ContainerTracker{
    41  		containerToArtifact: make(map[string]graph.Artifact),
    42  		deployedContainers:  make(map[string]Container),
    43  		notifier:            make(chan string, 1),
    44  	}
    45  }
    46  
    47  // Notifier returns the notifier channel for this tracker.
    48  // Used by the log streamer to be notified when a new container is
    49  // added to the tracker.
    50  func (t *ContainerTracker) Notifier() chan string {
    51  	return t.notifier
    52  }
    53  
    54  func (t *ContainerTracker) ArtifactForContainer(id string) graph.Artifact {
    55  	t.Lock()
    56  	defer t.Unlock()
    57  	return t.containerToArtifact[id]
    58  }
    59  
    60  // ContainerIdForImage returns the deployed container created from
    61  // the provided image, if it exists.
    62  func (t *ContainerTracker) ContainerForImage(image string) (Container, bool) {
    63  	t.Lock()
    64  	defer t.Unlock()
    65  	c, found := t.deployedContainers[image]
    66  	return c, found
    67  }
    68  
    69  // DeployedContainers returns the list of all containers deployed
    70  // during this run.
    71  func (t *ContainerTracker) DeployedContainers() map[string]Container {
    72  	return t.deployedContainers
    73  }
    74  
    75  // Reset resets the entire tracker when a new dev cycle starts.
    76  func (t *ContainerTracker) Reset() {
    77  	for c := range t.containers {
    78  		delete(t.containers, c)
    79  	}
    80  }
    81  
    82  // Add adds a container to the list.
    83  func (t *ContainerTracker) Add(artifact graph.Artifact, c Container) {
    84  	t.Lock()
    85  	defer t.Unlock()
    86  	t.deployedContainers[artifact.ImageName] = c
    87  	t.containerToArtifact[c.ID] = artifact
    88  	go func() {
    89  		t.notifier <- c.ID
    90  	}()
    91  }