// NewPluginConfigReaderFromReader encapsulates a WithError reader that
// is already attached to a config to read from.
509539
func NewPluginConfigReaderFromReader(reader PluginConfigReaderWithError) PluginConfigReader {
	errors := tgo.NewErrorStack()
	errors.SetFormat(tgo.ErrorStackFormatCSV)
	return PluginConfigReader{
		WithError: reader,
		Errors:    &errors,
	}
}
435559
func wrapAsPluginError(err error, msg string) error {
	return &pluginError{cause: errors.Wrap(err, msg)}
}
231916
func GetAdmissionPluginConfigurationFor(pluginCfg apiserver.AdmissionPluginConfiguration) (io.Reader, error) {
	// if there is a nest object, return it directly
	if pluginCfg.Configuration != nil {
		return bytes.NewBuffer(pluginCfg.Configuration.Raw), nil
	}
	// there is nothing nested, so we delegate to path
	if pluginCfg.Path != "" {
		content, err := ioutil.ReadFile(pluginCfg.Path)
		if err != nil {
			klog.Fatalf("Couldn't open admission plugin configuration %s: %#v", pluginCfg.Path, err)
			return nil, err
		}
		return bytes.NewBuffer(content), nil
	}
	// there is no special config at all
	return nil, nil
}
190424
func NewFromReader(reader io.Reader) (S3Cli, error) {
	bytes, err := ioutil.ReadAll(reader)
	if err != nil {
		return S3Cli{}, err
	}

	c := S3Cli{
		SSLVerifyPeer:   true,
		UseSSL:          true,
		MultipartUpload: true,
	}

	err = json.Unmarshal(bytes, &c)
	if err != nil {
		return S3Cli{}, err
	}

	if c.BucketName == "" {
		return S3Cli{}, errors.New("bucket_name must be set")
	}

	switch c.CredentialsSource {
	case StaticCredentialsSource:
		if c.AccessKeyID == "" || c.SecretAccessKey == "" {
			return S3Cli{}, errorStaticCredentialsMissing
		}
	case credentialsSourceEnvOrProfile:
		if c.AccessKeyID != "" || c.SecretAccessKey != "" {
			return S3Cli{}, newStaticCredentialsPresentError(credentialsSourceEnvOrProfile)
		}
	case NoneCredentialsSource:
		if c.AccessKeyID != "" || c.SecretAccessKey != "" {
			return S3Cli{}, newStaticCredentialsPresentError(NoneCredentialsSource)
		}

	case noCredentialsSourceProvided:
		if c.SecretAccessKey != "" && c.AccessKeyID != "" {
			c.CredentialsSource = StaticCredentialsSource
		} else if c.SecretAccessKey == "" && c.AccessKeyID == "" {
			c.CredentialsSource = NoneCredentialsSource
		} else {
			return S3Cli{}, errorStaticCredentialsMissing
		}
	default:
		return S3Cli{}, fmt.Errorf("Invalid credentials_source: %s", c.CredentialsSource)
	}

	switch Provider(c.Host) {
	case "aws":
		c.configureAWS()
	case "alicloud":
		c.configureAlicloud()
	case "google":
		c.configureGoogle()
	default:
		c.configureDefault()
	}

	return c, nil
}
535298
func NewWrapReadCloser(r io.Reader) io.ReadCloser {
	pr, pw := io.Pipe()
	go func() {
		_, _ = io.Copy(pw, r)
		pr.Close()
		pw.Close()
	}()
	return &wrapReadCloser{
		reader: pr,
		writer: pw,
	}
}
565514
func NewReader(r io.Reader, d EncodingHint) UTFReadCloser {
	var decoder *encoding.Decoder
	switch d {
	case UTF8:
		// Make a transformer that assumes UTF-8 but abides by the BOM.
		decoder = unicode.UTF8.NewDecoder()
	case UTF16LE:
		// Make an tranformer that decodes MS-Windows (16LE) UTF files:
		winutf := unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM)
		// Make a transformer that is like winutf, but abides by BOM if found:
		decoder = winutf.NewDecoder()
	case UTF16BE:
		// Make an tranformer that decodes UTF-16BE files:
		utf16be := unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM)
		// Make a transformer that is like utf16be, but abides by BOM if found:
		decoder = utf16be.NewDecoder()
	}

	// Make a Reader that uses utf16bom:
	if rc, ok := r.(readCloser); ok {
		rc.reader = transform.NewReader(rc.file, unicode.BOMOverride(decoder))
		return rc
	}

	return readCloser{
		reader: transform.NewReader(r, unicode.BOMOverride(decoder)),
	}
}
282117
func NewRegretableReaderCloserSize(rc io.ReadCloser, size int) *RegretableReaderCloser {
	return &RegretableReaderCloser{*NewRegretableReaderSize(rc, size), rc}
}
1198251
func NewConfigReadWriter(version int) (ConfigReadWriter, error) {
	switch version {
	case 1:
		return &v1{}, nil
	case 2:
		return &v2{}, nil
	}
	return nil, fmt.Errorf("Unknown version")
}
144070
func NewInteractiveDeferredLoadingClientConfig(loadingRules *ClientConfigLoadingRules, overrides *ConfigOverrides, fallbackReader io.Reader) ClientConfig {
	return DeferredLoadingClientConfig{loadingRules, overrides, fallbackReader}
}
1102584
func NewReadCloserOnError(r io.ReadCloser, notify func(error)) io.ReadCloser {
	return NewReadCloser(NewReaderOnError(r, notify), r)
}
975990
func (reader PluginConfigReaderWithError) GetFilterArray(key string, logger logrus.FieldLogger,
	defaultValue FilterArray) (FilterArray, error) {
	filters := []Filter{}

	modPlugins, err := reader.GetPluginArray(key, []Plugin{})
	if err != nil {
		return filters, err
	}
	if len(modPlugins) == 0 {
		return filters, nil
	}

	errors := tgo.NewErrorStack()
	errors.SetFormat(tgo.ErrorStackFormatCSV)

	for _, plugin := range modPlugins {
		if filter, isFilter := plugin.(Filter); isFilter {
			filters = append(filters, filter)
		} else {
			errors.Pushf("Plugin '%T' is not a valid filter", plugin)
			panic(errors.Top())
		}
	}

	return filters, errors.OrNil()
}
1021254
func ConfigFromJSON(reader io.Reader) (*Config, error) {
	jsonBytes, err := ioutil.ReadAll(reader)
	if err != nil {
		return nil, err
	}
	var obj map[string]interface{}
	if err := json.Unmarshal(jsonBytes, &obj); err != nil {
		return nil, err
	}
	return &Config{obj}, nil
}
1230012
func (i *gatewayHandler) newDagFromReader(r io.Reader) (ipld.Node, error) {
	// TODO(cryptix): change and remove this helper once PR1136 is merged
	// return ufs.AddFromReader(i.node, r.Body)
	return importer.BuildDagFromReader(
		i.node.DAG,
		chunker.DefaultSplitter(r))
}
1039311
func (reader *PluginConfigReader) GetPluginArray(key string, defaultValue []Plugin) []Plugin {
	value, err := reader.WithError.GetPluginArray(key, defaultValue)
	reader.Errors.Push(err)
	return value
}
127107
func NewPlugin() *Plugin {
	return &Plugin{
		Handler:  admission.NewHandler(admission.Create),
		features: utilfeature.DefaultFeatureGate,
	}
}
1322972
func (l *PluginLoader) validatePluginConfig(id PluginID, info *pluginInfo) error {
	var mErr multierror.Error

	// Check if a config is allowed
	if info.configSchema == nil {
		if info.config != nil {
			return fmt.Errorf("configuration not allowed but config passed")
		}

		// Nothing to do
		return nil
	}

	// Convert the schema to hcl
	spec, diag := hclspecutils.Convert(info.configSchema)
	if diag.HasErrors() {
		multierror.Append(&mErr, diag.Errs()...)
		return multierror.Prefix(&mErr, "failed converting config schema:")
	}

	// If there is no config, initialize it to an empty map so we can still
	// handle defaults
	if info.config == nil {
		info.config = map[string]interface{}{}
	}

	// Parse the config using the spec
	val, diag := hclutils.ParseHclInterface(info.config, spec, nil)
	if diag.HasErrors() {
		multierror.Append(&mErr, diag.Errs()...)
		return multierror.Prefix(&mErr, "failed parsing config:")
	}

	// Marshal the value
	cdata, err := msgpack.Marshal(val, val.Type())
	if err != nil {
		return fmt.Errorf("failed to msgpack encode config: %v", err)
	}

	// Store the marshalled config
	info.msgpackConfig = cdata

	// Dispense the plugin and set its config and ensure it is error free
	instance, err := l.Dispense(id.Name, id.PluginType, nil, l.logger)
	if err != nil {
		return fmt.Errorf("failed to dispense plugin: %v", err)
	}
	defer instance.Kill()

	b, ok := instance.Plugin().(base.BasePlugin)
	if !ok {
		return fmt.Errorf("dispensed plugin %s doesn't meet base plugin interface", id)
	}

	c := &base.Config{
		PluginConfig: cdata,
		AgentConfig:  nil,
		ApiVersion:   info.apiVersion,
	}

	if err := b.SetConfig(c); err != nil {
		return fmt.Errorf("setting config on plugin failed: %v", err)
	}
	return nil
}
42827
func NewReader(r io.Reader) (*Reader, error) {
	if r == nil {
		return nil, errors.New("Reader must not be nil")
	}

	return &Reader{r: r}, nil
}