// newLeaseDoc returns a valid lease document encoding the supplied lease and
// entry in the supplied namespace, or an error.
337597
func newLeaseDoc(namespace, name string, entry entry) (*leaseDoc, error) {
	doc := &leaseDoc{
		Id:        leaseDocId(namespace, name),
		Namespace: namespace,
		Name:      name,
		Holder:    entry.holder,
		Start:     toInt64(entry.start),
		Duration:  entry.duration,
		Writer:    entry.writer,
	}
	if err := doc.validate(); err != nil {
		return nil, errors.Trace(err)
	}
	return doc, nil
}
1074857
func newResourceDoc(stored storedResource) *resourceDoc {
	fullID := applicationResourceID(stored.ID)
	if stored.PendingID != "" {
		fullID = pendingResourceID(stored.ID, stored.PendingID)
	}
	return resource2doc(fullID, stored)
}
503392
func NewMemberSpaceLimitsAddCustomQuotaType(Description string) *MemberSpaceLimitsAddCustomQuotaType {
	s := new(MemberSpaceLimitsAddCustomQuotaType)
	s.Description = Description
	return s
}
146279
func NewLeases(storage storage.Interface, baseKey string, leaseTime time.Duration) Leases {
	return &storageLeases{
		storage:   storage,
		baseKey:   baseKey,
		leaseTime: leaseTime,
	}
}
493084
func NewLeaseCache(conf *LeaseCacheConfig) (*LeaseCache, error) {
	if conf == nil {
		return nil, errors.New("nil configuration provided")
	}

	if conf.Proxier == nil || conf.Logger == nil {
		return nil, fmt.Errorf("missing configuration required params: %v", conf)
	}

	if conf.Client == nil {
		return nil, fmt.Errorf("nil API client")
	}

	db, err := cachememdb.New()
	if err != nil {
		return nil, err
	}

	// Create a base context for the lease cache layer
	baseCtxInfo := cachememdb.NewContextInfo(conf.BaseContext)

	return &LeaseCache{
		client:      conf.Client,
		proxier:     conf.Proxier,
		logger:      conf.Logger,
		db:          db,
		baseCtxInfo: baseCtxInfo,
		l:           &sync.RWMutex{},
		idLocks:     locksutil.CreateLocks(),
	}, nil
}
700142
func ValidateLease(lease *coordination.Lease) field.ErrorList {
	allErrs := validation.ValidateObjectMeta(&lease.ObjectMeta, true, validation.NameIsDNSSubdomain, field.NewPath("metadata"))
	allErrs = append(allErrs, ValidateLeaseSpec(&lease.Spec, field.NewPath("spec"))...)
	return allErrs
}
138159
func NewNamespace() Namespace {
	return &namespace{
		taken: make(map[string]struct{}),
		gave:  make(map[string][]string),
	}
}
26073
func Lease(w http.ResponseWriter,
	req *api.CreateLeaseReq,
	clusterLister ClusterLister,
	services k8s.ServiceGetterUpdater,
	azureConfig *config.Azure,
	k8sServiceName string) {

	clusterMap, svc, err := getSvcsAndClusters(clusterLister, services, k8sServiceName)
	if err != nil {
		log.Printf("Error listing Azure clusters or talking to the k8s API -- %s", err)
		htp.Error(w, http.StatusInternalServerError, "Error listing Azure clusters or talking to the k8s API -- %s", err)
		return
	}

	leaseMap, err := leases.ParseMapFromAnnotations(svc.Annotations)
	if err != nil {
		log.Printf("Error parsing leases from Kubernetes annotations -- %s", err)
		htp.Error(w, http.StatusInternalServerError, "error parsing leases from Kubernetes annotations -- %s", err)
		return
	}

	availableCluster, err := searchForFreeCluster(clusterMap, leaseMap, req.ClusterRegex, req.ClusterVersion)
	if err != nil {
		switch e := err.(type) {
		case errNoAvailableOrExpiredClustersFound:
			log.Printf("No available clusters found")
			htp.Error(w, http.StatusConflict, "No available clusters found")
			return
		case errExpiredLeaseAzureMissing:
			log.Printf("Cluster %s has an expired lease but doesn't exist in Azure", e.clusterName)
			htp.Error(w, http.StatusInternalServerError, "Cluster %s has an expired lease but doesn't exist in Azure", e.clusterName)
			return
		default:
			log.Printf("Unknown error %s", e.Error())
			htp.Error(w, http.StatusInternalServerError, "Unknown error %s", e.Error())
			return
		}
	}

	// There is currently no way to fetch the kubeconfig from the Azure API
	// So we must scp the file off the master node
	kubeConfig, err := FetchKubeConfig(*availableCluster.MasterProfile.Fqdn)
	if err != nil {
		log.Printf("Error creating kubeconfig file for cluster %s -- %s", *availableCluster.Name, err)
		htp.Error(w, http.StatusInternalServerError, "Error creating kubeconfig file for cluster %s -- %s", *availableCluster.Name, err)
		return
	}
	newToken := uuid.NewUUID()
	kubeConfigStr, err := k8s.MarshalAndEncodeKubeConfig(kubeConfig)
	if err != nil {
		log.Printf("Error marshaling & encoding kubeconfig -- %s", err)
		htp.Error(w, http.StatusInternalServerError, "Error marshaling & encoding kubeconfig -- %s", err)
		return
	}

	resp := api.CreateLeaseResp{
		KubeConfigStr: kubeConfigStr,
		IP:            *availableCluster.MasterProfile.Fqdn,
		Token:         newToken.String(),
		ClusterName:   *availableCluster.Name,
	}

	leaseMap.CreateLease(newToken, leases.NewLease(*availableCluster.Name, req.ExpirationTime(time.Now())))
	if err := k8s.SaveAnnotations(services, svc, leaseMap); err != nil {
		log.Printf("Error saving new lease to Kubernetes annotations -- %s", err)
		htp.Error(w, http.StatusInternalServerError, "Error saving new lease to Kubernetes annotations -- %s", err)
		return
	}

	if err := json.NewEncoder(w).Encode(resp); err != nil {
		log.Printf("Error encoding json -- %s", err)
		htp.Error(w, http.StatusInternalServerError, "Error encoding json -- %s", err)
		return
	}
}
666770
func NewNamespaceKeyedIndexerAndReflector(lw ListerWatcher, expectedType interface{}, resyncPeriod time.Duration) (indexer Indexer, reflector *Reflector) {
	indexer = NewIndexer(MetaNamespaceKeyFunc, Indexers{"namespace": MetaNamespaceIndexFunc})
	reflector = NewReflector(lw, expectedType, indexer, resyncPeriod)
	return indexer, reflector
}
533761
func New(input string, capacity int) <-chan Token {
	if capacity < 0 {
		capacity = 0
	}
	_, c := lex(input, capacity)
	return c
}
513772
func (doc *DesignDoc) CreateDoc() (error, []byte) {

	buffer := &bytes.Buffer{}
	err := DESIGNTMPL.Execute(buffer, doc)
	return err, buffer.Bytes()
}
856791
func LookupLease(coll mongo.Collection, namespace, name string) (leaseDoc, error) {
	var doc leaseDoc
	err := coll.FindId(leaseDocId(namespace, name)).One(&doc)
	if err != nil {
		return leaseDoc{}, err
	}
	return doc, nil
}
565944
func (b *boundManager) leaseKey(leaseName string) lease.Key {
	return lease.Key{
		Namespace: b.namespace,
		ModelUUID: b.modelUUID,
		Lease:     leaseName,
	}
}
426351
func NewLifeCycle(installedChaincodes Enumerator) (*Lifecycle, error) {
	installedCCs, err := installedChaincodes.Enumerate()
	if err != nil {
		return nil, errors.Wrap(err, "failed listing installed chaincodes")
	}

	lc := &Lifecycle{
		installedCCs:           installedCCs,
		deployedCCsByChannel:   make(map[string]*chaincode.MetadataMapping),
		queryCreatorsByChannel: make(map[string]QueryCreator),
	}

	return lc, nil
}
154440
func NewNamedspacesController(service *goa.Service, db application.DB) *NamedspacesController {
	return &NamedspacesController{Controller: service.NewController("NamedspacesController"), db: db}
}
850247
func (ll *LeaseLock) Create(ler LeaderElectionRecord) error {
	var err error
	ll.lease, err = ll.Client.Leases(ll.LeaseMeta.Namespace).Create(&coordinationv1.Lease{
		ObjectMeta: metav1.ObjectMeta{
			Name:      ll.LeaseMeta.Name,
			Namespace: ll.LeaseMeta.Namespace,
		},
		Spec: LeaderElectionRecordToLeaseSpec(&ler),
	})
	return err
}
524012
func (s *leaseLister) Leases(namespace string) LeaseNamespaceLister {
	return leaseNamespaceLister{indexer: s.indexer, namespace: namespace}
}