github.com/containerd/nerdctl@v1.7.7/pkg/composer/up_volume.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 composer
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  
    23  	"github.com/containerd/log"
    24  	"github.com/containerd/nerdctl/pkg/labels"
    25  	"github.com/containerd/nerdctl/pkg/reflectutil"
    26  )
    27  
    28  func (c *Composer) upVolume(ctx context.Context, shortName string) error {
    29  	vol, ok := c.project.Volumes[shortName]
    30  	if !ok {
    31  		return fmt.Errorf("invalid volume name %q", shortName)
    32  	}
    33  	if vol.External.External {
    34  		// NOP
    35  		return nil
    36  	}
    37  
    38  	if unknown := reflectutil.UnknownNonEmptyFields(&vol, "Name"); len(unknown) > 0 {
    39  		log.G(ctx).Warnf("Ignoring: volume %s: %+v", shortName, unknown)
    40  	}
    41  
    42  	// shortName is like "db_data", fullName is like "compose-wordpress_db_data"
    43  	fullName := vol.Name
    44  	volExists, err := c.VolumeExists(fullName)
    45  	if err != nil {
    46  		return err
    47  	} else if !volExists {
    48  		log.G(ctx).Infof("Creating volume %s", fullName)
    49  		//add metadata labels to volume https://github.com/compose-spec/compose-spec/blob/master/spec.md#labels-2
    50  		createArgs := []string{
    51  			fmt.Sprintf("--label=%s=%s", labels.ComposeProject, c.project.Name),
    52  			fmt.Sprintf("--label=%s=%s", labels.ComposeVolume, shortName),
    53  			fullName,
    54  		}
    55  		if err := c.runNerdctlCmd(ctx, append([]string{"volume", "create"}, createArgs...)...); err != nil {
    56  			return err
    57  		}
    58  	}
    59  	return nil
    60  }