github.com/fafucoder/cilium@v1.6.11/cilium/cmd/prefilter_delete.go (about)

     1  // Copyright 2017 Authors of Cilium
     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  
    15  package cmd
    16  
    17  import (
    18  	"fmt"
    19  	"net"
    20  
    21  	"github.com/cilium/cilium/api/v1/models"
    22  
    23  	"github.com/spf13/cobra"
    24  )
    25  
    26  var preFilterDeleteCmd = &cobra.Command{
    27  	Use:   "delete",
    28  	Short: "Delete CIDR filters",
    29  	Run: func(cmd *cobra.Command, args []string) {
    30  		deleteFilters(cmd, args)
    31  	},
    32  }
    33  
    34  func init() {
    35  	preFilterCmd.AddCommand(preFilterDeleteCmd)
    36  	preFilterDeleteCmd.Flags().Uint64VarP(&revision, "revision", "", 0, "Update revision")
    37  	preFilterDeleteCmd.Flags().StringSliceVarP(&cidrs, "cidr", "", []string{}, "List of CIDR prefixes to delete")
    38  }
    39  
    40  func deleteFilters(cmd *cobra.Command, args []string) {
    41  	spec := &models.PrefilterSpec{
    42  		Revision: int64(revision),
    43  		Deny:     cidrs,
    44  	}
    45  	for _, cidr := range cidrs {
    46  		_, _, err := net.ParseCIDR(cidr)
    47  		if err != nil {
    48  			Fatalf("Cannot parse CIDR \"%s\": %s", cidr, err)
    49  		}
    50  	}
    51  	if _, err := client.DeletePrefilter(spec); err != nil {
    52  		Fatalf("Cannot delete prefilter: %s", err)
    53  	} else {
    54  		fmt.Printf("Deleted %d prefilter entries\n", len(spec.Deny))
    55  	}
    56  }