pkg: add a function to generate chain names

Adds a function to generate chain names for use in iptables and
ports all drivers to use that function. Also adds tests for the
said function.
This commit is contained in:
Abhishek Chanda
2016-03-22 17:16:59 -07:00
committed by Stefan Junker
parent bacaa11d2d
commit c33daf6706
6 changed files with 57 additions and 7 deletions

20
pkg/utils/utils.go Normal file
View File

@ -0,0 +1,20 @@
package utils
import (
"crypto/sha512"
"fmt"
)
// 29 - len('CNI') - 2*len('-')
const maxNameLen = 16
// Generates a chain name to be used with iptables.
// Ensures that the generated name is less than
// 29 chars in length
func FormatChainName(name string, id string) string {
h := sha512.Sum512([]byte(id))
if len(name) > maxNameLen {
return fmt.Sprintf("CNI-%s-%x", name[:len(name)-maxNameLen], h[:8])
}
return fmt.Sprintf("CNI-%s-%x", name, h[:8])
}