func OverwriteBootstrapPolicy(optsGetter restoptions.Getter, policyFile, createBootstrapPolicyCommand string, change bool, out io.Writer) error { if !change { fmt.Fprintf(out, "Performing a dry run of policy overwrite:\n\n") } mapper := cmdclientcmd.ShortcutExpander{RESTMapper: kubectl.ShortcutExpander{RESTMapper: registered.RESTMapper()}} typer := kapi.Scheme clientMapper := resource.ClientMapperFunc(func(mapping *meta.RESTMapping) (resource.RESTClient, error) { return nil, nil }) r := resource.NewBuilder(mapper, typer, clientMapper, kapi.Codecs.UniversalDecoder()). FilenameParam(false, false, policyFile). Flatten(). Do() if r.Err() != nil { return r.Err() } policyStorage, err := policyetcd.NewStorage(optsGetter) if err != nil { return err } policyRegistry := policyregistry.NewRegistry(policyStorage) policyBindingStorage, err := policybindingetcd.NewStorage(optsGetter) if err != nil { return err } policyBindingRegistry := policybindingregistry.NewRegistry(policyBindingStorage) clusterPolicyStorage, err := clusterpolicyetcd.NewStorage(optsGetter) if err != nil { return err } clusterPolicyRegistry := clusterpolicyregistry.NewRegistry(clusterPolicyStorage) clusterPolicyBindingStorage, err := clusterpolicybindingetcd.NewStorage(optsGetter) if err != nil { return err } clusterPolicyBindingRegistry := clusterpolicybindingregistry.NewRegistry(clusterPolicyBindingStorage) ruleResolver := rulevalidation.NewDefaultRuleResolver( policyListerNamespacer{registry: policyRegistry}, policyBindingListerNamespacer{registry: policyBindingRegistry}, clusterpolicyregistry.ReadOnlyClusterPolicy{Registry: clusterPolicyRegistry}, clusterpolicybindingregistry.ReadOnlyClusterPolicyBinding{Registry: clusterPolicyBindingRegistry}, ) roleStorage := rolestorage.NewVirtualStorage(policyRegistry, ruleResolver) roleBindingStorage := rolebindingstorage.NewVirtualStorage(policyBindingRegistry, ruleResolver) clusterRoleStorage := clusterrolestorage.NewClusterRoleStorage(clusterPolicyRegistry, clusterPolicyBindingRegistry) clusterRoleBindingStorage := clusterrolebindingstorage.NewClusterRoleBindingStorage(clusterPolicyRegistry, clusterPolicyBindingRegistry) return r.Visit(func(info *resource.Info, err error) error { if err != nil { return err } template, ok := info.Object.(*templateapi.Template) if !ok { return errors.New("policy must be contained in a template. One can be created with '" + createBootstrapPolicyCommand + "'.") } runtime.DecodeList(template.Objects, kapi.Codecs.UniversalDecoder()) for _, item := range template.Objects { switch t := item.(type) { case *authorizationapi.Role: ctx := kapi.WithNamespace(kapi.NewContext(), t.Namespace) if change { roleStorage.Delete(ctx, t.Name, nil) if _, err := roleStorage.CreateRoleWithEscalation(ctx, t); err != nil { return err } } else { fmt.Fprintf(out, "Overwrite role %s/%s\n", t.Namespace, t.Name) if s, err := describe.DescribeRole(t); err == nil { fmt.Fprintf(out, "%s\n", s) } } case *authorizationapi.RoleBinding: ctx := kapi.WithNamespace(kapi.NewContext(), t.Namespace) if change { roleBindingStorage.Delete(ctx, t.Name, nil) if _, err := roleBindingStorage.CreateRoleBindingWithEscalation(ctx, t); err != nil { return err } } else { fmt.Fprintf(out, "Overwrite role binding %s/%s\n", t.Namespace, t.Name) if s, err := describe.DescribeRoleBinding(t, nil, nil); err == nil { fmt.Fprintf(out, "%s\n", s) } } case *authorizationapi.ClusterRole: ctx := kapi.WithNamespace(kapi.NewContext(), t.Namespace) if change { clusterRoleStorage.Delete(ctx, t.Name, nil) if _, err := clusterRoleStorage.CreateClusterRoleWithEscalation(ctx, t); err != nil { return err } } else { fmt.Fprintf(out, "Overwrite role %s/%s\n", t.Namespace, t.Name) if s, err := describe.DescribeRole(authorizationapi.ToRole(t)); err == nil { fmt.Fprintf(out, "%s\n", s) } } case *authorizationapi.ClusterRoleBinding: ctx := kapi.WithNamespace(kapi.NewContext(), t.Namespace) if change { clusterRoleBindingStorage.Delete(ctx, t.Name, nil) if _, err := clusterRoleBindingStorage.CreateClusterRoleBindingWithEscalation(ctx, t); err != nil { return err } } else { fmt.Fprintf(out, "Overwrite role binding %s/%s\n", t.Namespace, t.Name) if s, err := describe.DescribeRoleBinding(authorizationapi.ToRoleBinding(t), nil, nil); err == nil { fmt.Fprintf(out, "%s\n", s) } } default: return fmt.Errorf("only roles and rolebindings may be created in this mode, not: %v", reflect.TypeOf(t)) } } if !change { fmt.Fprintf(out, "To make the changes described above, pass --force\n") } return nil }) }
func OverwriteBootstrapPolicy(optsGetter restoptions.Getter, policyFile, createBootstrapPolicyCommand string, change bool, out io.Writer) error { if !change { fmt.Fprintf(out, "Performing a dry run of policy overwrite:\n\n") } mapper := cmdclientcmd.ShortcutExpander{RESTMapper: kubectl.ShortcutExpander{RESTMapper: registered.RESTMapper()}} typer := kapi.Scheme clientMapper := resource.ClientMapperFunc(func(mapping *meta.RESTMapping) (resource.RESTClient, error) { return nil, nil }) r := resource.NewBuilder(mapper, typer, clientMapper, kapi.Codecs.UniversalDecoder()). FilenameParam(false, false, policyFile). Flatten(). Do() if r.Err() != nil { return r.Err() } policyStorage, err := policyetcd.NewStorage(optsGetter) if err != nil { return err } policyRegistry := policyregistry.NewRegistry(policyStorage) policyBindingStorage, err := policybindingetcd.NewStorage(optsGetter) if err != nil { return err } policyBindingRegistry := policybindingregistry.NewRegistry(policyBindingStorage) clusterPolicyStorage, err := clusterpolicyetcd.NewStorage(optsGetter) if err != nil { return err } clusterPolicyRegistry := clusterpolicyregistry.NewRegistry(clusterPolicyStorage) clusterPolicyBindingStorage, err := clusterpolicybindingetcd.NewStorage(optsGetter) if err != nil { return err } clusterPolicyBindingRegistry := clusterpolicybindingregistry.NewRegistry(clusterPolicyBindingStorage) ruleResolver := rulevalidation.NewDefaultRuleResolver( policyListerNamespacer{registry: policyRegistry}, policyBindingListerNamespacer{registry: policyBindingRegistry}, clusterpolicyregistry.ReadOnlyClusterPolicy{Registry: clusterPolicyRegistry}, clusterpolicybindingregistry.ReadOnlyClusterPolicyBinding{Registry: clusterPolicyBindingRegistry}, ) roleStorage := rolestorage.NewVirtualStorage(policyRegistry, ruleResolver, nil, authorizationapi.Resource("role")) roleBindingStorage := rolebindingstorage.NewVirtualStorage(policyBindingRegistry, ruleResolver, nil, authorizationapi.Resource("rolebinding")) clusterRoleStorage := clusterrolestorage.NewClusterRoleStorage(clusterPolicyRegistry, clusterPolicyBindingRegistry, nil) clusterRoleBindingStorage := clusterrolebindingstorage.NewClusterRoleBindingStorage(clusterPolicyRegistry, clusterPolicyBindingRegistry, nil) return r.Visit(func(info *resource.Info, err error) error { if err != nil { return err } template, ok := info.Object.(*templateapi.Template) if !ok { return errors.New("policy must be contained in a template. One can be created with '" + createBootstrapPolicyCommand + "'.") } runtime.DecodeList(template.Objects, kapi.Codecs.UniversalDecoder()) // For each object, we attempt the following to maximize our ability to persist the desired objects, while minimizing etcd write thrashing: // 1. Create the object (no-ops if the object already exists) // 2. If the object already exists, attempt to update the object (no-ops if an identical object is already persisted) // 3. If we encounter any error updating, delete and recreate errs := []error{} for _, item := range template.Objects { switch t := item.(type) { case *authorizationapi.Role: ctx := kapi.WithNamespace(kapi.NewContext(), t.Namespace) if change { // Attempt to create _, err := roleStorage.CreateRoleWithEscalation(ctx, t) // Unconditional replace if it already exists if kapierrors.IsAlreadyExists(err) { _, _, err = roleStorage.UpdateRoleWithEscalation(ctx, t) } // Delete and recreate as a last resort if err != nil { roleStorage.Delete(ctx, t.Name, nil) _, err = roleStorage.CreateRoleWithEscalation(ctx, t) } // Gather any error if err != nil { errs = append(errs, err) } } else { fmt.Fprintf(out, "Overwrite role %s/%s\n", t.Namespace, t.Name) if s, err := describe.DescribeRole(t); err == nil { fmt.Fprintf(out, "%s\n", s) } } case *authorizationapi.RoleBinding: ctx := kapi.WithNamespace(kapi.NewContext(), t.Namespace) if change { // Attempt to create _, err := roleBindingStorage.CreateRoleBindingWithEscalation(ctx, t) // Unconditional replace if it already exists if kapierrors.IsAlreadyExists(err) { _, _, err = roleBindingStorage.UpdateRoleBindingWithEscalation(ctx, t) } // Delete and recreate as a last resort if err != nil { roleBindingStorage.Delete(ctx, t.Name, nil) _, err = roleBindingStorage.CreateRoleBindingWithEscalation(ctx, t) } // Gather any error if err != nil { errs = append(errs, err) } } else { fmt.Fprintf(out, "Overwrite role binding %s/%s\n", t.Namespace, t.Name) if s, err := describe.DescribeRoleBinding(t, nil, nil); err == nil { fmt.Fprintf(out, "%s\n", s) } } case *authorizationapi.ClusterRole: ctx := kapi.WithNamespace(kapi.NewContext(), t.Namespace) if change { // Attempt to create _, err := clusterRoleStorage.CreateClusterRoleWithEscalation(ctx, t) // Unconditional replace if it already exists if kapierrors.IsAlreadyExists(err) { _, _, err = clusterRoleStorage.UpdateClusterRoleWithEscalation(ctx, t) } // Delete and recreate as a last resort if err != nil { clusterRoleStorage.Delete(ctx, t.Name, nil) _, err = clusterRoleStorage.CreateClusterRoleWithEscalation(ctx, t) } // Gather any error if err != nil { errs = append(errs, err) } } else { fmt.Fprintf(out, "Overwrite role %s/%s\n", t.Namespace, t.Name) if s, err := describe.DescribeRole(authorizationapi.ToRole(t)); err == nil { fmt.Fprintf(out, "%s\n", s) } } case *authorizationapi.ClusterRoleBinding: ctx := kapi.WithNamespace(kapi.NewContext(), t.Namespace) if change { // Attempt to create _, err := clusterRoleBindingStorage.CreateClusterRoleBindingWithEscalation(ctx, t) // Unconditional replace if it already exists if kapierrors.IsAlreadyExists(err) { _, _, err = clusterRoleBindingStorage.UpdateClusterRoleBindingWithEscalation(ctx, t) } // Delete and recreate as a last resort if err != nil { clusterRoleBindingStorage.Delete(ctx, t.Name, nil) _, err = clusterRoleBindingStorage.CreateClusterRoleBindingWithEscalation(ctx, t) } // Gather any error if err != nil { errs = append(errs, err) } } else { fmt.Fprintf(out, "Overwrite role binding %s/%s\n", t.Namespace, t.Name) if s, err := describe.DescribeRoleBinding(authorizationapi.ToRoleBinding(t), nil, nil); err == nil { fmt.Fprintf(out, "%s\n", s) } } default: errs = append(errs, fmt.Errorf("only roles and rolebindings may be created in this mode, not: %v", reflect.TypeOf(t))) } } if !change { fmt.Fprintf(out, "To make the changes described above, pass --force\n") } return kerrors.NewAggregate(errs) }) }