1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
package apicaps
import (
"fmt"
"sort"
"strings"
pb "github.com/moby/buildkit/util/apicaps/pb"
"github.com/pkg/errors"
)
type PBCap = pb.APICap
// ExportedProduct is the name of the product using this package.
// Users vendoring this library may override it to provide better versioning hints
// for their users (or set it with a flag to buildkitd).
var ExportedProduct string
// CapStatus defines the stability properties of a capability
type CapStatus int
const (
// CapStatusStable refers to a capability that should never be changed in
// backwards incompatible manner unless there is a serious security issue.
CapStatusStable CapStatus = iota
// CapStatusExperimental refers to a capability that may be removed in the future.
// If incompatible changes are made the previous ID is disabled and new is added.
CapStatusExperimental
// CapStatusPrerelease is same as CapStatusExperimental that can be used for new
// features before they move to stable.
CapStatusPrerelease
)
// CapID is type for capability identifier
type CapID string
// Cap describes an API feature
type Cap struct {
ID CapID
Name string // readable name, may contain spaces but keep in one sentence
Status CapStatus
Enabled bool
Deprecated bool
SupportedHint map[string]string
DisabledReason string
DisabledReasonMsg string
DisabledAlternative string
}
// CapList is a collection of capability definitions
type CapList struct {
m map[CapID]Cap
}
// Init initializes definition for a new capability.
// Not safe to be called concurrently with other methods.
func (l *CapList) Init(cc ...Cap) {
if l.m == nil {
l.m = make(map[CapID]Cap, len(cc))
}
for _, c := range cc {
l.m[c.ID] = c
}
}
// All reports the configuration of all known capabilities
func (l *CapList) All() []pb.APICap {
out := make([]pb.APICap, 0, len(l.m))
for _, c := range l.m {
out = append(out, pb.APICap{
ID: string(c.ID),
Enabled: c.Enabled,
Deprecated: c.Deprecated,
DisabledReason: c.DisabledReason,
DisabledReasonMsg: c.DisabledReasonMsg,
DisabledAlternative: c.DisabledAlternative,
})
}
sort.Slice(out, func(i, j int) bool {
return out[i].ID < out[j].ID
})
return out
}
// CapSet returns a CapSet for an capability configuration
func (l *CapList) CapSet(caps []pb.APICap) CapSet {
m := make(map[string]*pb.APICap, len(caps))
for _, c := range caps {
if c.ID != "" {
c := c // capture loop iterator
m[c.ID] = &c
}
}
return CapSet{
list: l,
set: m,
}
}
// CapSet is a configuration for detecting supported capabilities
type CapSet struct {
list *CapList
set map[string]*pb.APICap
}
// Supports returns an error if capability is not supported
func (s *CapSet) Supports(id CapID) error {
err := &CapError{ID: id}
c, ok := s.list.m[id]
if !ok {
return errors.WithStack(err)
}
err.Definition = &c
state, ok := s.set[string(id)]
if !ok {
return errors.WithStack(err)
}
err.State = state
if !state.Enabled {
return errors.WithStack(err)
}
return nil
}
// Contains checks if cap set contains cap. Note that unlike Supports() this
// function only checks capability existence in remote set, not if cap has been initialized.
func (s *CapSet) Contains(id CapID) bool {
_, ok := s.set[string(id)]
return ok
}
// CapError is an error for unsupported capability
type CapError struct {
ID CapID
Definition *Cap
State *pb.APICap
}
func (e CapError) Error() string {
if e.Definition == nil {
return fmt.Sprintf("unknown API capability %s", e.ID)
}
typ := ""
if e.Definition.Status == CapStatusExperimental {
typ = "experimental "
}
if e.Definition.Status == CapStatusPrerelease {
typ = "prerelease "
}
name := ""
if e.Definition.Name != "" {
name = "(" + e.Definition.Name + ")"
}
b := &strings.Builder{}
fmt.Fprintf(b, "requested %sfeature %s %s", typ, e.ID, name)
if e.State == nil {
fmt.Fprint(b, " is not supported by build server")
if hint, ok := e.Definition.SupportedHint[ExportedProduct]; ok {
fmt.Fprintf(b, " (added in %s)", hint)
}
fmt.Fprintf(b, ", please update %s", ExportedProduct)
} else {
fmt.Fprint(b, " has been disabled on the build server")
if e.State.DisabledReasonMsg != "" {
fmt.Fprintf(b, ": %s", e.State.DisabledReasonMsg)
}
}
return b.String()
}
|