github.com/uber/kraken@v0.1.4/lib/dockerregistry/metadata.go (about)

     1  // Copyright (c) 2016-2019 Uber Technologies, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  package dockerregistry
    15  
    16  import (
    17  	"fmt"
    18  	"regexp"
    19  	"strings"
    20  	"time"
    21  
    22  	"github.com/uber/kraken/lib/store/metadata"
    23  )
    24  
    25  const (
    26  	_startedAtSuffix = "_startedat"
    27  	_startedAtLayout = time.RFC3339
    28  )
    29  
    30  func init() {
    31  	metadata.Register(regexp.MustCompile(_startedAtSuffix), &startedAtMetadataFactory{})
    32  
    33  	// TODO(evelynl): use _ instead of /, otherwise it won't support reload.
    34  	metadata.Register(regexp.MustCompile("_hashstates/\\w+/\\w+$"), &hashStateMetadataFactory{})
    35  }
    36  
    37  type startedAtMetadataFactory struct{}
    38  
    39  func (f startedAtMetadataFactory) Create(suffix string) metadata.Metadata {
    40  	return &startedAtMetadata{}
    41  }
    42  
    43  // startedAtMetadata is used to remove uploads that have been active for too long.
    44  type startedAtMetadata struct {
    45  	time time.Time
    46  }
    47  
    48  func newStartedAtMetadata(t time.Time) *startedAtMetadata {
    49  	return &startedAtMetadata{t}
    50  }
    51  
    52  func (s *startedAtMetadata) GetSuffix() string {
    53  	return _startedAtSuffix
    54  }
    55  
    56  func (s *startedAtMetadata) Movable() bool {
    57  	return false
    58  }
    59  
    60  func (s *startedAtMetadata) Serialize() ([]byte, error) {
    61  	return []byte(s.time.Format(_startedAtLayout)), nil
    62  }
    63  
    64  func (s *startedAtMetadata) Deserialize(b []byte) error {
    65  	t, err := time.Parse(_startedAtLayout, string(b))
    66  	if err != nil {
    67  		return err
    68  	}
    69  	s.time = t
    70  	return nil
    71  }
    72  
    73  type hashStateMetadataFactory struct{}
    74  
    75  func (f hashStateMetadataFactory) Create(suffix string) metadata.Metadata {
    76  	parts := strings.Split(suffix, "/")
    77  	if len(parts) != 3 {
    78  		return nil
    79  	}
    80  	algo := parts[1]
    81  	offset := parts[2]
    82  	return newHashStateMetadata(algo, offset)
    83  }
    84  
    85  // hashStateMetadata stores partial hash result of upload data for resumable upload.
    86  // Docker registry double writes to a writer and digester, and the digester
    87  // generates this snapshot.
    88  type hashStateMetadata struct {
    89  	algo    string
    90  	offset  string
    91  	content []byte
    92  }
    93  
    94  func newHashStateMetadata(algo, offset string) *hashStateMetadata {
    95  	return &hashStateMetadata{
    96  		algo:   algo,
    97  		offset: offset,
    98  	}
    99  }
   100  
   101  func (h *hashStateMetadata) GetSuffix() string {
   102  	return fmt.Sprintf("_hashstates/%s/%s", h.algo, h.offset)
   103  }
   104  
   105  func (h *hashStateMetadata) Movable() bool {
   106  	return false
   107  }
   108  
   109  func (h *hashStateMetadata) Serialize() ([]byte, error) {
   110  	return h.content, nil
   111  }
   112  
   113  func (h *hashStateMetadata) Deserialize(b []byte) error {
   114  	h.content = b
   115  	return nil
   116  }
   117  
   118  func (h *hashStateMetadata) dockerPath() string {
   119  	return fmt.Sprintf("hashstates/%s/%s", h.algo, h.offset)
   120  }