...

Source file src/k8s.io/kubernetes/pkg/util/iptables/save_restore.go

Documentation: k8s.io/kubernetes/pkg/util/iptables

     1  //go:build linux
     2  // +build linux
     3  
     4  /*
     5  Copyright 2014 The Kubernetes Authors.
     6  
     7  Licensed under the Apache License, Version 2.0 (the "License");
     8  you may not use this file except in compliance with the License.
     9  You may obtain a copy of the License at
    10  
    11      http://www.apache.org/licenses/LICENSE-2.0
    12  
    13  Unless required by applicable law or agreed to in writing, software
    14  distributed under the License is distributed on an "AS IS" BASIS,
    15  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16  See the License for the specific language governing permissions and
    17  limitations under the License.
    18  */
    19  
    20  package iptables
    21  
    22  import (
    23  	"bytes"
    24  	"fmt"
    25  
    26  	"k8s.io/apimachinery/pkg/util/sets"
    27  )
    28  
    29  // MakeChainLine return an iptables-save/restore formatted chain line given a Chain
    30  func MakeChainLine(chain Chain) string {
    31  	return fmt.Sprintf(":%s - [0:0]", chain)
    32  }
    33  
    34  // GetChainsFromTable parses iptables-save data to find the chains that are defined. It
    35  // assumes that save contains a single table's data, and returns a set with keys for every
    36  // chain defined in that table.
    37  func GetChainsFromTable(save []byte) sets.Set[Chain] {
    38  	chainsSet := sets.New[Chain]()
    39  
    40  	for {
    41  		i := bytes.Index(save, []byte("\n:"))
    42  		if i == -1 {
    43  			break
    44  		}
    45  		start := i + 2
    46  		save = save[start:]
    47  		end := bytes.Index(save, []byte(" "))
    48  		if end == -1 {
    49  			// shouldn't happen, but...
    50  			break
    51  		}
    52  		chain := Chain(save[:end])
    53  		chainsSet.Insert(chain)
    54  		save = save[end:]
    55  	}
    56  	return chainsSet
    57  }
    58  

View as plain text